Most read:
Popular archives:
JavaWorld's new look is here!
We've upgraded the site with a fresh look-and-feel, improved topical navigation, better search, new features, and expanded
community platform. Learn more about the changes to JavaWorld.
| Oracle Compatibility Developer's Guide |
| The Explosion in DBMS Choice |
Page 4 of 6
<c:if test="${status.totalVisits == 1000000}" var="visits">
You are the millionth visitor to our site! Congratulations!
</c:if>
Below we show JSTL's support for switching logic with c:choose, c:when, and c:otherwise. A set of c:when actions may be included within a choose tag; if any of the expressions in the c:when blocks evaluate to true, the following tests in the c:choose action are not evaluated. If none of the tests in the c:when blocks evaluate to true, c:otherwise action's contents, if present, are evaluated:
<c:choose>
<c:when test="${item.type == 'book'}">
...
</c:when>
<c:when test="${item.type == 'electronics'}">
...
</c:when>
<c:when test="${item.type == 'toy'}">
...
</c:when>
<c:otherwise>
...
</c:otherwise>
</c:choose>
The c:foreach action provides an easy way to iterate through a collection's elements. If you want to only iterate over part of the collection,
you can specify starting and ending indices and an increment value with the optional begin, end, and step attributes, respectively. In the following example, we iterate through the contents of a collection in the variable customerNames; in each loop, the next element is put in the variable name and evaluated in the c:forEach action's body.
<table>
<c:forEach var="name" items="${customerNames}">
<tr><td><c:out value="${name}"/></td></tr>
</c:forEach>
</table>
Remember Java's StringTokenizer class? With the c:forTokens action, you can obtain similar functionality in JSTL. This fragment iterates through items in the items String attribute using the delimiter(s) defined in the delims attribute. Note that the items attribute doesn't need to be a string literal; it can be any valid EL expression:
<table>
<c:forTokens items="47,52,53,55,46,22,16,2" delim="," var="dailyPrice">
<tr><td><c:out value="${dailyPrice}"/></td></tr>
</c:forTokens>
</table>
In the following complete JSTL page, I list all passed parameters that have been passed to the page. The param and paramValues implicit objects are Java Map collections that map keys to one or more values. In this example, we find the key for each MapEntry in the collection, which is the parameter name, and use the key to look up all the parameter values associated with the key:
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<body>
<head>
<title>Parameter Listing Example</title>
</head>
<br>
<b>Parameter values passed to this page for each parameter: </b>
<table border="2">
<c:forEach var="current" items="${param}">
<tr>
<td>
<b><c:out value="${current.key}" /></b>
</td>
<c:forEach var="aVal" items="${paramValues[current.key]}">
<td>
<c:out value="${aVal}" />
</td>
</c:forEach>
</tr>
</c:forEach>
</table>
</body>
</html>
There are some other important core tag library actions we need to cover. One potential problem area in Webpage implementation
relates to URL encoding. Without URL encoding, certain characters in URLs that pass between Webpages, like spaces, can confuse
Web servers. URL encoding ensures that these special characters are replaced with characters that are not confusing. The following
example defines a URL in the variable myUrl, which consists of a URL and a set of parameters. The URL action (note we refer to "action" in the JSTL sense here) ensures
that all characters are properly encoded: