[ Team LiB ] |
![]() ![]() |
Recipe 23.3 Using the Core JSTL TagsProblemYou want to use the core JSTL tags in a JSP. SolutionUse the taglib directive with the core uri attribute value to make the tags available in the JSP. DiscussionThis recipe demonstrates several JSTL tags that you use all the time: c:set, c:out, c:forEach, and c:if. Here are the tag summaries:
Example 23-1 is a helper class that I find necessary to properly return a String array of TimeZone IDs to the JSP in Example 23-2. Example 23-1. A helper class to help display TimeZone IDspackage com.jspservletcookbook;
import java.util.TimeZone;
public class ZoneWrapper {
public ZoneWrapper( ){}
public String[] getAvailableIDs( ){
return TimeZone.getAvailableIDs( );
}
}
Example 23-2 shows how to use a number of the core JSTL tags. The code uses the jsp:useBean standard action to create ZoneWrapper (Example 23-1) and java.util.Date objects for use by the tags. Example 23-2. Using core JSTL 1.0 tags in a JSP<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <jsp:useBean id="zone" class="com.jspservletcookbook.ZoneWrapper" /> <jsp:useBean id="date" class="java.util.Date" /> <html> <head><title>Using the Core JSTL tags</title></head> <body> <h2>Here are the available Time Zone IDs on your system</h2> <c:if test="${date.time != 0}" > <c:out value= "Phew, time has not stopped yet...<br /><br />" escapeXml="false"/> </c:if> <%-- The variable 'zones' contains a String array of TimeZone IDs; it is stored as a 'session' object attribute. The '${zone.availableIDs}' expression is the equivalent of calling the ZoneWrapper.getAvailableIDs( ) method --%> <c:set var="zones" value="${zone.availableIDs}" scope="session" /> <c:forEach var="id" items="${zones}"> <c:out value="${id}<br />" escapeXml="false" /> </c:forEach> </body> </html> The c:if tag uses an EL phrase to test whether the Date object's getTime( ) method returns a value that is not zero (of course it does! I'm just demonstrating how to use the c:if tag). ${date.time != 0} The prior code represents a boolean expression that returns true if Date.getTime( ) is greater than zero. If true, then the code executes the nested c:out tag, which writes a message that the client's browser displays.
Example 23-2 sets an object attribute to session scope. This object is a String[] type containing time zone IDs, such as "Pacific/Tahiti." The c:forEach tag then iterates over all of these array members, displaying each ID with the c:out tag: <c:forEach var="id" items="${zones}"> <c:out value="${id}<br />" escapeXml="false" /> </c:forEach> The var attribute of the c:forEach tag stores the current array member as c:forEach cycles over the collection. The c:out tag uses an EL expression to access the value of the current array member: <c:out value="${id}<br />" escapeXml="false" /> If you do not give the escapeXml attribute a false value when using c:out , the character entity codes shown in Table 23-3 will display instead of the escaped characters.
Figure 23-1 shows a part of the JSP using the code in Example 23-2. Figure 23-1. A JSP using the various core tags to display time zone IDs![]() See AlsoRecipe 6.8 on including content in a JSP with the c:url tag; the Jakarta Project's Taglibs site: http://jakarta.apache.org/taglibs/index.html; the Sun Microsystems JSTL information page: http://java.sun.com/products/jsp/jstl/; Recipe 23.4 and Recipe 23.5 on using XML-related tags; Recipe 23.6 on using the formatting tags; Recipe 23.7 and Recipe 23.8 on the JSTL's SQL features; Recipe 23.9-Recipe 23.14 on using the EL to access scoped variables, cookies, and JavaBean properties. |
[ Team LiB ] |
![]() ![]() |