background image

The forTokens tag

<< Iterator Tags | URL Tags >>
<< Iterator Tags | URL Tags >>

The forTokens tag

Here's the shopping cart iteration from the preceding section, now with the forEach tag:
<c:forEach var=
"item" items="${sessionScope.cart.items}">
...
<tr>
<td align=
"right" bgcolor="#ffffff">
${item.quantity}
</td>
...
</c:forEach>
The forTokens tag is used to iterate over a collection of tokens separated by a delimiter.
Similarly to the value attribute of the c:set tag (see
"Variable Support Tags" on page 205
), the
items
attribute of forEach and forTokens can also take a deferred value expression so that
JavaServer Faces tags can be included within these tags.
As described in
"Variable Support Tags" on page 205
, JavaServer Faces technology (see
Chapter 10, "JavaServer Faces Technology"
) supports a multiphase life cycle. Therefore, any
JavaServer Faces component tags that are included in the forEach tag or the forTokens tag
must have access to the variable referenced by the items attribute at different phases of the life
cycle, not just during the rendering phase. Consider the following code:
<c:forEach var=
"book" items="#{BooksBean.books}">
...
<h:inputText id=
"quantity" value="#{book.quantity}"/>
...
</c:forEach>
The items attribute uses a deferred value expression, which means that the book variable it
references is available not only during the rendering phase of the JavaServer Faces life cycle but
also during the later stages of the life cycle. Therefore, whatever values the user enters into the
quantity
component tags are updated to the external data object during the appropriate stage
of the life cycle.
If the expression referenced by the items attribute used immediate evaluation syntax then the
book
variable would be available only when the component is rendered during the render
response phase. This would prevent the values the user enters into the components from being
converted, validated, or updated to the external data object during the later phases of the life
cycle. The JavaServer Faces version of Duke's Bookstore includes a forEach tag on its
tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookcatalog.jsp
page.
Core Tag Library
Chapter 7 · JavaServer Pages Standard Tag Library
209