Get the Object Index From ArrayList with JSTL

11,230

Use varStatus attribute.

<c:forEach items="${searchResults}" var="contact" varStatus="loop">
  <div style="padding: 5px;">
  ${loop.index} - 
  ${contact.firstName} ${contact.lastName}
  <br>
  ${contact.primaryPhone}
 </div>
</c:forEach>
Share:
11,230
ryandlf
Author by

ryandlf

Updated on June 13, 2022

Comments

  • ryandlf
    ryandlf almost 2 years

    Is it possible to get the "row" index from an arraylist using JSTL?

    <c:forEach items="${searchResults}" var="contact">
    <div style="padding: 5px;">
    ${contact.firstName} ${contact.lastName}
    <br>
    ${contact.primaryPhone}
    </div>
    </c:forEach>
    

    My intention is to set a hyperlink that contains each items ID on each row so the user can click and display a popup, or another page and easily retrieve just the single object from the arraylist without going back to the database and setting another session object etc.