Pregunta

I have simple question: Is possible in jsp make variable in foreach where I will store number of iteretion? The question also include how to do it? There is example how I wanna use it. It's this kind of problem that you will solve in few seconds if you know the answer, but its hard to find if you even know how to call it so the simple example explain everything ...
Description to the example: In arraylist list are href values for my tags which are references for pages. NUMBER should represent my variable - something like list iterator. So I will get: Page 1, Page 2, Page 3, ... Page x, where x is number of items in list list.

<c:forEach items="${requestScope.list}" var="listItem">
<a href="<c:out value="${listItem}" />">Page NUMBER</a>
</c:forEach>
¿Fue útil?

Solución

Use the 'varStatus' attribute, that will give you the counter.

<c:forEach items="${requestScope.list}" var="listItem" varStatus="counter" >
     <a href="<c:out value="${listItem}" />"><c:out value="${counter}" /></a>
</c:forEach> 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top