• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL forEach

 
Jordi Monn�
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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� ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic