wood burning stoves 2.0*
The moose likes JSP and the fly likes JSTL forEach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL forEach" Watch "JSTL forEach" New topic
Author

JSTL forEach

Jordi Monn�
Greenhorn

Joined: Feb 28, 2006
Posts: 21
Hi!

I'm doing some tests with JSTL 1.1 to learn it.

I have this sample:

<c:set var="i" value="0"/>
<c:forEach var="u" items="${bookusuaris.usuaris}">
<c:set var="i" value="${i + 1}"/>
<option value="${i}">${u.nom}</option>
</c:forEach>

I need to know the value of i (the value of the return), is there a way to do it without use <c:set var="i" value="${i + 1}"/> ?

Thanks to all
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
If you want to keep track of an index/counter as you go through the forEach loop, you can use the for loops "status" variable.



The varStatus attribute specifies the variable name to use for the loop status variable (I call it status normally).
${status.index} = 0 based index
${status.count} = 1 based index

it also has methods ${status.first} and ${status.last} which tells you if a record is first/last (can be useful sometimes)

Cheers,
evnafets
Jordi Monn�
Greenhorn

Joined: Feb 28, 2006
Posts: 21
Perfect your way is the way that i wanted.

For more info about status of iteration, check

Jakarta JSTL API Documents

Thanks for your help
[ April 03, 2006: Message edited by: Jordi Monn� ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL forEach
 
Similar Threads
JSTL 1.1 - Get value from method of an Obj and set it to a variable
Nested C:forEach varStatus ??
Nested hashmap tags for dynamic pulldowns
Problem with JSTL c:out tag
struts form