File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL forEach" Watch "JSTL forEach" New topic

JSTL forEach

Jordi Monn�

Joined: Feb 28, 2006
Posts: 21

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>

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

Joined: Jul 06, 2005
Posts: 1580
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)

Jordi Monn�

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:
subject: JSTL forEach
It's not a secret anymore!