Get index of a foreach - Struts, JSTL, FreeMarker

How to get the index of the current iteration of a foreach loop in Struts2, JSTL, FreeMarker

Struts 2

<s:iterator value="userList" status="status" >    
   
    <s:property value="#status.index" />
    <s:property value="firstname" />

</s:iterator>

JSTL

<c:forEach items="${userList}" var="u" varStatus="status">

    <c:out value="${status.index}" />
    <c:out value="${u.firstname}" />

</c:forEach>

FreeMarker

<#list userList as u>

    ${u_index}
    ${u.firstname}
  
</#list>
Backend code
List<User> userList = new ArrayList<User>();

userList.add(new User("sam", "jay"));
userList.add(new User("sameera", "jaya"));

1 comment: