user3657834
user3657834

Reputation: 259

Is there a way to get the index of each item in a list being passed to JSTL c:forEach

If I use the JSTL forEach loop to display a list of all returned items from my controller, Is there a way to output an index number for each item that is returned in the list?

<c:forEach var="item" items="#{Controller.allItems}" >

    <tr><td>{index number here???} : #{item.name}</td></tr>    
</c:forEach>  

Upvotes: 0

Views: 155

Answers (1)

Dennis Kriechel
Dennis Kriechel

Reputation: 3749

That's what the attribute varStatus is for:

<c:forEach var="item" items="#{Controller.allItems}" varStatus="status" >
     <tr><td>${status.index} : #{item.name}</td></tr>    
</c:forEach>  

VarStatus contains many other Attributes as well:

  • begin
  • end
  • index
  • step
  • even
  • odd
  • first
  • last

Upvotes: 3

Related Questions