| This Chapter | |
| - | Chapter 7: JSTL |
| - | Introducing JSTL |
| - | General-Purpose Actions |
| - | Conditional Actions |
| - | Iterator Actions |
| - | Formatting Actions |
| - | Functions |
| - | Summary |
Iterator actions are useful when you need to iterate a number of times or over a collection of objects. JSTL provides two tags that perform iterator actions, forEach and forTokens, both of which are discussed in the following sections.
forEach iterates a body content a number of times or iterates a collection of objects. Objects that can be iterated over include all implementations of java.util.Collection and java.util.Map, and arrays of objects or primitive types. You can also iterate over a java.util.Iterator and java.util.Enumeration, but you should not use Iterator or Enumeration in more than one action because neither Iterator nor Enumeration will be reset.
The syntax for forEach has two forms. The first form is for repeating the body content a fixed number of times:
<c:forEach [var="varName"] begin="begin" end="end" step="step"> body content </c:forEach>
The second form is used to iterate over a collection of objects:
<c:forEach items="collection" [var="varName"] [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]> body content </c:forEach>
The body content is JSP. The list of the forEach tag’s attributes is given in Table 7.6.
| Attribute | Type | Description |
|---|---|---|
| var | String | The name of the scoped variable that references the current item of the iteration. |
| items+ | Any of the supported type. | Collections of objects to iterate over. |
| varStatus | String | The name of the scoped variable that holds the status of the iteration. The value is of type javax.servlet.jsp.jstl.core.LoopTagStatus. |
| begin+ | int | If items is specified, iteration begins at the item located at the specified index, in which the first item of the collection has an index of 0. If items is not specified, iteration begins with the index set at the value specified. If specified, the value of begin must be equal to or greater than zero. |
| end+ | int | If items is specified, iteration ends at the item located at the specified index (inclusive). If items is not specified, iteration ends when index reaches the value specified. |
| step+ | int | Iteration will process only every step items of the collection, starting with the first one. If present, the value of step must be equal to or greater than 1. |
Table 7.6: The forEach Tag’s attributes
For example, the following forEach tag displays “1, 2, 3, 4, 5”.
<c:forEach var="x" begin="1" end="5">
<c:out value="${x}"/>,
</c:forEach>
And, the following forEach tag iterates the ArrayList returned by the phones property of the scoped object address.
<c:forEach var="phone" items="${address.phones}">
<c:out value="${phone}"/><br/>
</c:forEach>
For each iteration, the forEach tag creates a scoped variable whose name is specified by the var attribute. In this case, the scoped variable is named phone. The out tag within the forEach tag is used to display the value of phone. The scoped variable is only available from within the beginning and closing forEach tags, and will be removed right before the closing forEach tag.
The following forEach tag is similar to the previous one. However, it uses the varStatus variable, whose type is javax.servlet.jsp.jstl.core.LoopTagStatus. The LoopTagStatus interface has the count property that returns the "count" of the current round of iteration. The value of status.count is 1 for the first iteration, 2 for the second iteration, and so on. By testing the remainder of status.count%2, you know whether the tag is processing an even-numbered or odd-numbered element.
In addition, the following forEach tag is used in conjunction with the choose, when, and otherwise tags to display the items in an HTML table. The choose tag is used to test the value of varStatus.count and if it is evenly divisible by 2 (which indicates it is an even number), the table row is displayed in red (color code ff0000). Otherwise, the table row is displayed in green (color code 00ff00).
<table>
<c:forEach var="phone" items="${address.phones}"
varStatus="status">
<c:choose>
<c:when test="${status.count%2==0}">
<tr bgcolor="#ff0000">
</c:when>
<c:otherwise>
<tr bgcolor="#00ff00">
</c:otherwise>
</c:choose>
<td><c:out value="${phone}"/></td>
</tr>
</c:forEach>
</table>
You use the forTokens tag to iterate over tokens that are separated by the specified delimiters. The syntax for this action is as follows:
<c:forTokens items="stringOfTokens" delims="delimiters" [var="varName"] [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]> body content </c:forTokens>
The body content is JSP. The list of the forTokens tag’s attributes is given in Table 7.7.
| Attribute | Type | Description |
|---|---|---|
| var | String | The name of the scoped variable that references the current item of the iteration. |
| items+ | String | The string of tokens to iterate over. |
| varStatus | String | The name of the scoped variable that holds the status of the iteration. The value is of type javax.servlet.jsp.jstl.core.LoopTagStatus. |
| begin+ | int | The start index of the iteration, where index is zero-based. If specified, begin must be 0 or greater. |
| end+ | int | The end index of the iteration, where index is zero-based. |
| step+ | int | Iteration will process only every step tokens of the string, starting with the first one. If specified, step must be 1 or greater. |
| delims+ | String | The set of delimiters. |
Table 7.7: The forTokens tag’s attributes
Here is an example of forTokens:
<c:forTokens var="item" items="Argentina,Brazil,Chile" delims=",">
<c:out value="${item}"/><br/>
</c:forTokens>
When pasted in a JSP, the preceding forTokens will result in the following:
Argentina Brazil Chile