Apache Struts 2 Documentation > Home > FAQs > Cookbook > Iterator tag examples |

Apache Struts 2 Documentation
Iterator tag examples
Edit Page Browse Space Add Page Add News
Added by adrian, last edited by Ted Husted on Sep 02, 2006 (view change)

This follows on from Struts Tags which you should read first, but beware of references to '0' and 'that'; what you really want is 'top', as illustrated below. (I finally worked this out from the source code - hopefully this page means you won't have to.)

#### Referencing the current value

The simple examples print out values from the list using the property tag, which uses the value at the top of the stack by default:

    Days:
    <ul>
    <ww:iterator value="days">
        <li><ww:property/>
    </ww:iterator>
    </ul>

But if you're doing anything other than print the value, you probably need to refer to it specifically. Do this:

    Most days:
    <ul>
    <ww:iterator value="days">
        <ww:if test="top != 'Monday'">
            <li><ww:property/>
        </ww:if>
    </ww:iterator>
    </ul>

#### Iterating over a list of objects

    <ww:iterator value="employees">
        <ww:property value="name"/> is the <ww:property value="jobTitle"/><br>
    </ww:iterator>

For 'name' and 'jobTitle' you could be more explicit and write 'top.name' and 'top.jobTitle', as 'top' refers to the object on the top of the stack. It's not necessary here, but it is in the next example.

#### Iterating over a list of lists

    <table>
        <ww:iterator value="grid">
            <tr>
            <ww:iterator value="top">
                <td><ww:property/></td>
            </ww:iterator>
            </tr>
        </ww:iterator>
    </table>

The trick here is to use 'top' as the value for the inner iterator. This example probably uses a two-dimensional array, but you can use the pattern for any list of lists.

#### A more complex example

In this example, 'countries' is a list of country objects, each of which has a name and a list of cities. Each city has a name.

    <ww:iterator value="countries">
        <ww:iterator value="cities">
            <ww:property value="name"/>, <ww:property value="[1].name"/><br>
        </ww:iterator>
    </ww:iterator>

The output looks like

    Wellington, New Zealand
    Auckland, New Zealand
    Moscow, Russia
    Glasgow, Scotland
    Edinburgh, Scotland
    Stockholm, Sweden

Both the country and city objects have a 'name' property. As you'd expect, the reference to 'name' on its own gives you the city name. To access the country name - effectively "hidden" by the city name - we refer to a specific position on the stack: '1'. The top of the stack, position 0, contains the current city, pushed on by the inner iterator; position 1 contains the current country, pushed there by the outer iterator.

Actually, as Patrick points out in his comment on Iteration Tags, the 'n' notation refers to a sub-stack beginning at position n, not just the object at position n. Thus '0' is the whole stack and '1' is everything except the top object. In our example, we could have been more specific about getting the country name and said '1.top.name'.

#### Misc

If no value is specified, iterator will try to grap object from the 'top' of the stack. If it is not iterable, ClassCastException will be thrown in the process. @see com.opensymphony.webwork.views.jsp.IteratorTag#doStartTag