Tuesday, June 01, 2004

Map iterations using c:forEach

Using the c:forEach custom tag, it is easy to iterate over collections including map's.
Altough you have to keep in mind that the resulting value is a MapItem.

The MapItem has 2 properties named key and value.
These properties can be treated as javabeans theirselves, hence its properties being retrieved according to the javabean specification.
Hereunder follows a sample that iterates over a map, the key is of type String and the value of a type that complies to the javabean specification:

<c:forEach var="item" items="myMap">
<c:out value="${item.key}"/>
<c:out value="${item.value.givenName}"/>
<c:out value="${item.value.surName}"/>
The output of the above sample is:

3Johnny jr.Doe