0

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>  
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3657834
  • 249
  • 4
  • 9
  • 21
  • 2
    http://stackoverflow.com/a/11699191/1391249 – Tiny Dec 01 '15 at 15:38
  • 1
    Possible duplicate of [Representation of a simple for loop in JSTL/EL](http://stackoverflow.com/questions/11698993/representation-of-a-simple-for-loop-in-jstl-el) – BalusC Dec 01 '15 at 15:53

1 Answers1

3

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
Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62