JSTL and Struts logic tags iteration quick reference
October 19th, 2009 in Java/J2EE. 1 commentIterating through collections in jsp using JSTL and Struts tags can be confusing, especially since the attributes can be a little hard to figure out. Here’s a ready reference of some iteration examples that may help.
For our examples here let us assume that ClassA and ClassB are associated in the following way, and that these two classes have the getters and setters defined for the properties shown here.

1. Code examples of looping using JSTL
- Loop through a List of objects and access (print) a property of every object contained at an even index:
<jsp:useBean id="beanA" class="ClassA" scope="request"/> <c:forEach items="${beanA.listA2}" var="eachItem" varStatus="status" > <c:if test='${(status.index)%2 == "0"}' > <c:out value="${eachItem.aProperty}" /> </c:if> </c:forEach> -
Same as the above but using the step attribute instead of the if condition:
<c:forEach items="${beanA.listA2}" step="2" var="eachItem" varStatus="status" > <c:out value="${eachItem.aProperty}" /> </c:forEach> -
Loop through a map and print key and value:
<jsp:useBean id="beanA" class="ClassA" scope="request"/> <c:forEach items="${beanA.mapA1}" var="eachItem"> <c:out value="${eachItem.key}" />:<c:out value="${eachItem.value}" /> </c:forEach> - Loop through a HashMap containing objects (as value) and access a simple property contained in each object:
<jsp:useBean id="beanA" class="ClassA" scope="request"/> <c:forEach items="${beanA.mapA2}" var="eachItem"> <c:out value="${eachItem.value.aProperty}" />; </c:forEach> - Nested Loops: Loop through a hashmap of objects and for each of these objects loop through a HashMap property of the object.
<jsp:useBean id="beanA" class="ClassA" scope="request"/> <c:forEach items="${beanA.mapA2}" var="eachItem"> <c:set var="bObj" value="${eachItem.value}" /> <c:forEach items="${eachItem.value.mapB1}" var="anItem"> <c:out value="${anItem.key}" /> : <c:out value="${anItem.value}" /> </c:forEach> </c:forEach>
2. Code Examples of looping using Struts Iterate tag
We assume here ClassA is the form bean (or is in page scope).
- Loop through a list:
<logic:iterate id="eachItem" name="ClassA" property="listA1"> <bean:write name="eachItem"/> </logic:iterate>
- Loop through a list and print a property contained in each item in the list
<logic:iterate id="eachItem" name="ClassA" property="listA2" > <bean:write name="eachItem" property="aProperty"/> </logic:iterate>
-
Loop through a hashmap and display key value pairs:
<logic:iterate id="eachItem" name="ClassA" property="mapA1"> key:<bean:write name="eachItem" property="key"/> value:<bean:write name="eachItem" property="value"/> </logic:iterate>
- Loop through a HashMap containing objects (as value) and access a simple property contained in each object.
<logic:iterate id="eachItem" name="ClassA" property="mapA2"> <bean:define id="bObj" name="eachItem" property="value" /> <bean:define id="akey" name="eachItem" property="key" /> aProperty for object at key <bean:write name="akey" /> is <bean:write name="bObj" property="aProperty"/> </logic:iterate>
- Nested loop: Looping though a HashMap contained in a HashMap:
<logic:iterate id="eachItem" name="ClassA" property="mapA2"> <bean:define id="eachBObj" name="eachItem" property="value" /> <logic:iterate name="eachBObj" id="eachMapB1Entry" property="mapB1"> <bean:write name="eachMapB1Entry" property="key"/>:<bean:write name="eachMapB1objInB" property="value"/> </logic:iterate> </logic:iterate>
-
Loop demonstrating how to access indexes using indexId:
<logic:iterate id="eachItem" indexId="index" name="ClassA" property="listA2"> <c:if test="${index % 2 == 0}"> <bean:write name="eachItem" property="aProperty"/> </c:if> </logic:iterate>
Tags: Java


November 9th, 2011 at 12:57 am says:
Thank’s a lot your codes made me realize what I forgot. =)