Struts2 Control Tags - Using iterator Tag

January 23rd, 2008 | by Admin |

Download Source | View Demo

iterator tag can be used to loop over a collection of objects. The only requirement is that the collection should be of type java.util.Collection or java.util.iterator.

Here is a sample use,

Inside the iterator, the top of the value stack is the current object in the loop. Hence when you access “name”, it is actually “current_list_object.name”.

The current object is pushed to the value stack using the “id” field as the key. Hence referring “#userobj.name” is same as “name”. Similarly status field value (in this case user_stat) is the key in which the iterator status object is pushed. Hence you can check for an even row using the expression #user_stat.even which returns a boolean value.

Check out the following sample which demonstrates the use of iterator tag. In this IterateDemo action class populates dummy customer data is a list and passes it to iteratedemo.jsp. Using Struts2 iterator, the customer listing is printed out. Note the use of iterator status for alternate coloring of the rows. This sample also shows the use of stylesheets.

IterateDemo.java

Customer.java

iteratedemo.jsp

struts.xml

Parameters for iterator tag

Name Required Default Evaluated Type Description
id false false String Value stack key in which the current object is pushed
value false false String The list being iterated
status false false false String Value stack key in which the iterator status is stored

One Response to “Struts2 Control Tags - Using iterator Tag”

  1. By subramani on Apr 9, 2008

    Thanks for ur brief explanation in how to use the iterator tag,its quite useful in my project

Post a Comment