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

  1. <s:iterator value="userList" status="status" >      
  2.      
  3.     <s:property value="#status.index" />  
  4.     <s:property value="firstname" />  
  5.   
  6. </s:iterator>  

JSTL

  1. <c:forEach items="${userList}" var="u" varStatus="status">  
  2.   
  3.     <c:out value="${status.index}" />  
  4.     <c:out value="${u.firstname}" />  
  5.   
  6. </c:forEach>  

FreeMarker

  1. <#list userList as u>  
  2.   
  3.     ${u_index}  
  4.     ${u.firstname}  
  5.     
  6. </#list>  
Backend code
  1. List<User> userList = new ArrayList<User>();  
  2.   
  3. userList.add(new User("sam""jay"));  
  4. userList.add(new User("sameera""jaya"));  

1 comment: