background image

Iterator Tags

<< Conditional Tags | The forTokens tag >>
<< Conditional Tags | The forTokens tag >>

Iterator Tags

<c:choose>
<c:when test=
"${customer.category == 'trial'}" >
...
</c:when>
<c:when test=
"${customer.category == 'member'}" >
...
</c:when>
<c:when test=
"${customer.category == 'preferred'}" >
...
</c:when>
<c:otherwise>
...
</c:otherwise>
</c:choose>
The choose, when, and otherwise tags can be used to construct an if-then-else statement as
follows:
<c:choose>
<c:when test=
"${count == 0}" >
No records matched your selection.
</c:when>
<c:otherwise>
${count} records matched your selection.
</c:otherwise>
</c:choose>
Iterator Tags
The forEach tag allows you to iterate over a collection of objects. You specify the collection
using the items attribute, and the current item is available through a variable named by the var
attribute.
A large number of collection types are supported by forEach, including all implementations of
java.util.Collection
and java.util.Map. If the items attribute is of type java.util.Map,
then the current item will be of type java.util.Map.Entry, which has the following properties:
key
: The key under which the item is stored in the underlying Map
value
: The value that corresponds to the key
Arrays of objects as well as arrays of primitive types (for example, int) are also supported. For
arrays of primitive types, the current item for the iteration is automatically wrapped with its
standard wrapper class (for example, Integer for int, Float for float, and so on).
Implementations of java.util.Iterator and java.util.Enumeration are supported, but
they must be used with caution. Iterator and Enumeration objects can't be reset, so they
should not be used within more than one iteration tag. Finally, java.lang.String objects can
be iterated over if the string contains a list of comma-separated values (for example:
Monday,Tuesday,Wednesday,Thursday,Friday
).
Core Tag Library
The Java EE 5 Tutorial · September 2007
208