GeeCON Prague 2014*
The moose likes JSP and the fly likes ForEach: how to get array data ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "ForEach: how to get array data ?" Watch "ForEach: how to get array data ?" New topic
Author

ForEach: how to get array data ?

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
I have an array, I want to fill all data into two columns in a table. If we just use scriplet, it will be simple. but how to do it in JSTL.


in the JSTL, the tricky thing is , how to get array[i], array[i+1] in the ForEach ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61298
    
  66

This test page:



should give you enough clues to figure out your answer on your own.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798


I almost got there using your way. Now only problem left is , how to
<c:if test="${loop.index+1 < xyz.length }"> , the "xyz.length" has error message . I try to set variable, still not work.

Need your help.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61298
    
  66

As per forum instructions, you should have included what version of JSP and JSTL you are using.

If using JSP 2.0 and JSTL 1.1, you can use the fn:length() function to determine the length of a collection.

In JSP 1.x, not so much...
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
I am using JSTL1.0.
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Do you mean I have to mix java scriplet with JSTL ?

like

<%
int length = array.length;
pageContext.put("length", length);
%>
<c:forEach ....>
<c:if test="${index < length">
</c:forEach>

Sounds like NOT work.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61298
    
  66

What version of JSP are you using? Please don't make me ask again.
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
JSTL 1.0, JSP 2.0
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61298
    
  66

If you are using JSP 2.0, you need to be using JSTL 1.1. Otherwise, the JSTL and the JSP engine will have fights over who's job it is to evaluate the EL.

Replace your JSTL 1.0 jars with JSTL 1.1, update your JSTL URIs and you'll be able to use the JSTL functions, including fn:length().
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
OK, how to confirm what version of JSTL, JSP we are using ? Do we have DOS command to do this ?

My web server is Tomcat 5.0, it should use JSP 2.0, right ?

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61298
    
  66

Yes, Tomcat 5 is JSP 2.0-capable and will run as a JSP 2.0 engine if the web app is declared as a Servlets 2.4 app in the deployment descriptor.

What URIs are you using for the JSTL? They differ between JSTL 1.0 and 1.1.
[ July 13, 2006: Message edited by: Bear Bibeault ]
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Thanks, Bear,

My web.xml declares

It means, it is using JSP 1.0, right?

in JSTL, declares

Here, I am confusing JSTL version. Sounds like it is using JSTL 1.0, but why it is using 'web-jsptaglibrary_1_2.dtd' ?

Thanks.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
>Here, I am confusing JSTL version. Sounds like it is using JSTL 1.0,
> but why it is using 'web-jsptaglibrary_1_2.dtd' ?
That is correct. The DTD is defined as part of the JSP spec. JSTL1.0 was written for JSP1.2 - it is a custom tag library for JSP1.2.

Your web.xml is declared as using Version 2.3.
You have JSTL 1.0 installed.
So your web application will act like a JSP1.2 container - ie EL will only be evaluated in the JSTL tags. Also you can't use EL functions.

If you want to use JSP2.0/JSTL1.1, you need to update your web.xml to declared itself as version2.4, and also install JSTL1.1 jars instead of JSTL1.0.

As an aside, the code will work perfectly fine without that if statement.
Just try it:

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Bear Bibeault wrote:This test page:



should give you enough clues to figure out your answer on your own.


Hi Bear, I know you posted this in 2006 but it just helped me figure out how to access array indexes using the jstl foreach loop! Thank you very much
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61298
    
  66

My pleasure!
 
GeeCON Prague 2014
 
subject: ForEach: how to get array data ?