Struts 2 Iterator Tag Example
In Stuts 2 the iterator tag is used to loop over a collection of objects. The iterator tag can iterate over any Collection, Map, Enumeration, Iterator, or array.
<table class="songTable">
<tr class="even">
<td><b>Title</b></td>
<td><b>Genre</b></td>
</tr>
<s:iterator value="songs" status="songStatus">
<tr
class="<s:if test="#songStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td><s:property value="title" /></td>
<td><s:property value="genre" /></td>
</tr>
</s:iterator>
</table>
We use the iterator tag to iterator over the collection of songs. Here the Song property is of type ArrayList. To know more about the iterator status we can create an instance of the IteratorStatus object by specifying a value to the status attribute of the iterator tag. The newly created instance will be placed in the ActionContext which can be refered using the the following OGNL expression #statusName.
The table show the different properties of the IteratorStatus object.
|