Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i get last element iterator struts2

I'm looking for ideas to show only last element of iterator using struts 2

i have this code :

<s:iterator value="listuser"  status="userStatus">
<tr class="<s:if test="#userStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td><s:property value="nameuser" /></td>
//  and others
</s:iterator>
like image 964
stoner Avatar asked Sep 23 '13 18:09

stoner


2 Answers

The Struts2 Iterator Status not only provide odd and even methods, it provides also a last and first method.

<s:iterator value="listuser"  status="userStatus">
  <s:if test="#userStatus.last == true ">
    <td><s:property value="nameuser" /></td>
  </s:if>
</s:iterator>
like image 179
Johannes Avatar answered Sep 28 '22 22:09

Johannes


Something like this will do the trick :

<s:if test="#userStatus.index==listuser.size()-1">
           //Show the last element here
</s:if>

[UPDATE]

As per Quaternion's comment, here's how to get it in one line :

 <s:set name="lastUser" value="listuser[listuser.size()-1]"/>

Above we set the last element in lastUser and here's how to use it else-where in the same page.

  <s:property value="#lastUser.name"/>
like image 20
coding_idiot Avatar answered Sep 28 '22 23:09

coding_idiot