user1097772
user1097772

Reputation: 3529

Simple variable(counter) in foreach in jsp

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>

Upvotes: 1

Views: 1114

Answers (1)

Sashi
Sashi

Reputation: 2425

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> 

Upvotes: 2

Related Questions