[ Team LiB ] |
![]() ![]() |
Recipe 23.15 Using JSTL FunctionsProblemYou want to use the built-in functions included with JSTL 1.1. SolutionUse the proper taglib directive (with the uri value of "http://java.sun.com/jsp/jstl/functions") and prefix (e.g., the fn: in fn:contains) in your JSP. DiscussionThe JSTL 1.1 and its EL includes a nifty new functions library. These tags allow JSP developers to call built-in functions to handle and return values from Strings, arrays, Maps, and Collections. The nature of these functions will be familiar to anyone who has worked with java.lang.String and its numerous methods (see Table 23-4). Functions represent an evolution of JSTL from involving a collection of custom tags to giving you the ability to make function calls embedded inside template text. Here is the setup that you need to use JSTL functions in your JSPs:
Example 23-17 shows the new taglib uri and prefix values to use with the functions library. This JSP uses the String "I am a test String" as input to four of the available functions: fn:length( ) , fn:contains( ), fn:toUpperCase( ), and fn:split( ). Example 23-17. A JSP that uses JSTL 1.1 functions<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head><title>Using the JSTL functions</title></head> <body> <h2>Using various JSTL 1.1 functions</h2> <c:set var="tempStr" value="I am a test String"/> The length of the test String: ${fn:length(tempStr)}<br /> Does the test String contain "test"? ${fn:contains(tempStr,"test")}<br /> Putting the String into upper case using fn:toUpperCase( ): ${fn:toUpperCase(tempStr)}<br /> Splitting the String into a String array using fn:split( ), and returning the array length: ${fn:length(fn:split(tempStr," "))} </body> </html> JSTL 1.1 function calls can be intermingled with template text, as in Example 23-17. Surround the function calls with the EL delimiters ("${...}"), and make sure to use the fn: prefix, as in ${fn:toUpperCase(tempStr)}. Example 23-18 shows how you can change web.xml to the servlet API 2.4 version, so that the JSP 2.0 container interprets the EL functions in your code.
If you stick with the servlet API 2.3 deployment descriptor, then the JSP 2.0 container will not evaluate the EL expressions and function calls. Using the old servlet 2.3 deployment descriptor "turns off" EL evaluation by the JSP container; consequently, you cannot use the functions library or include EL syntax in template text. This automatic disabling of EL expressions by the JSP container is designed as a way of easing the migration of existing JSP pages to JSP 2.0. In short, a JSP that includes the JSTL 1.0 usages and is associated with a servlet 2.3 deployment descriptor works the same under a JSP 2.0 container. However, you may want to use the new functions! Therefore, Example 23-18 shows how to migrate to the servlet 2.4 version of web.xml. Example 23-18. Change web.xml to servlet API 2.4 to use JSTL 1.1 features<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4"
>
<!-- REST OF DEPLOYMENT DESCRIPTOR ELEMENTS -->
</web-app>
Example 23-18 alters the web-app element to include the required attributes of the servlet 2.4 deployment descriptor (See Chapter 1). The rest of web.xml can remain as it appeared using the servlet 2.3 DTD. Table 23-4 describes the purpose of each function that the JSTL 1.1 includes in its function library.
Figure 23-10 shows the web browser output of a JSP that uses various members of the JSTL 1.1 functions library. Figure 23-10. A JSP displays the results of using some JSTL 1.1 functions![]() See AlsoSun Microsystem's JSTL information page: http://java.sun.com/products/jsp/jstl/; Recipe 23.2 on using the core tags; Recipe 23.3 and Recipe 23.4 on using XML-related tags; Recipe 23.5 on formatting dates and numbers; Recipe 23.6 and Recipe 23.7 on the JSTL's SQL features; Recipe 23.8 on accessing scoped variables; Recipe 23.9 on using the EL with request parameters; Recipe 23.10 and Recipe 23.11 on using the EL with request headers; Recipe 23.12 on finding out information about cookies; Recipe 23.13 on using the EL to access JavaBean properties. |
[ Team LiB ] |
![]() ![]() |