background image

Flow Control Tags

<< Core Tags | Internationalization Tag Library >>
<< Core Tags | Internationalization Tag Library >>

Flow Control Tags

If you want to store a String, you must use x:out within c:set. The x:out tag converts the
node to a String, and c:set then stores the String as an EL variable. For example,
tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookdetails.jsp
stores an EL variable containing a book price, which is later provided as the value of a fmt tag, as
follows:
<c:set var=
"price">
<x:out select=
"$abook/price"/>
</c:set>
<h4><fmt:message key=
"ItemPrice"/>:
<fmt:formatNumber value=
"${price}" type="currency"/>
The other option, which is more direct but requires that the user have more knowledge of
XPath, is to coerce the node to a String manually by using XPath's string function.
<x:set var=
"price" select="string($abook/price)"/>
Flow Control Tags
The XML flow control tags parallel the behavior described in
"Flow Control Tags" on page 206
for XML data streams.
The JSP page
tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookcatalog.jsp uses
the forEach tag to display all the books contained in booklist as follows:
<x:forEach var=
"book"
select=
"$applicationScope:booklist/books/*">
<tr>
<c:set var=
"bookId">
<x:out select=
"$book/@id"/>
</c:set>=
<td bgcolor=
"#ffffaa">
<c:url var=
"url"
value=
"/bookdetails" >
<c:param name=
"bookId" value="${bookId}" />
<c:param name=
"Clear" value="0" />
</c:url>
<a href=
"${url}">
<strong><x:out select=
"$book/title"/>&nbsp;
</strong></a></td>
<td bgcolor=
"#ffffaa" rowspan=2>
<c:set var=
"price">
<x:out select=
"$book/price"/>
</c:set>
<fmt:formatNumber value=
"${price}" type="currency"/>
XML Tag Library
The Java EE 5 Tutorial · September 2007
214