Using the JSP Standard Tag Library (JSTL) with JSF Berner Fachhochschule-Technik und Informatik Overview and Installation � Advanced Web Technologies Installing JSTL 8) Including JSTL in JSF Iteration Tags � Logic Tags � Source: The “if” Tag http://www.coreservlets.com/JSF-Tutorial/ The “choose” Tag Fall Semester 09-10 Other Tags � Advanced Web Technologies 8) Including JSTL in JSF Advanced Web Technologies 8) Including JSTL in JSF 1 2 Overview and Installation JSTL Overview ◮ JSTL was based on the Struts looping and logic tags ◮ JSTL is not part of the JSP 1.2 or 2.0 Specs • It is a separate specification that requires a separate download • Available only in servers that support servlets 2.3 and JSP 1.2 or later. Cannot be retrofitted into JSP 1.1. ◮ The JSTL expression language is part of JSP 2.0, JSP 2.1, and JSF ◮ The JSTL Specification is available in PDF • http://jcp.org/en/jsr/detail?id=52 Advanced Web Technologies 8) Including JSTL in JSF Advanced Web Technologies 8) Including JSTL in JSF Overview and Installation Overview and Installation 3 4
Installing JSTL The JSTL Expression Language ◮ Some servers come with JSTL preinstalled ◮ Accessed via $ { expression } • E.g., MyFaces ◮ Official reference implementation ◮ Similar to JavaScript and XPath • http://jakarta.apache.org/taglibs/doc/ ◮ Provides shorthand notation to access: standard-doc/intro.html • Attributes of standard servlet objects ◮ JSTL (like JSP) is a specification, not an • Bean properties implementation • Map, List, and Array elements • Code is portable but not all versions are equal ◮ Is standard part of JSP 2.0 and 2.1 • Speed, tools, and development environments vary • In JSTL, EL can be used only in attributes of JSTL tags ◮ To install: • In JSP 2.0, the EL can be used anywhere • Download 1.1 zip file • web.xml element and page directive attribute let you disable • Unzip into directory of your choice. the EL for backward compatibility • Copy install_dir/lib/jstl.jar and ◮ Covered in separate lecture install_dir/lib/standard.jar to the WEB-INF/lib directory of your Web application Advanced Web Technologies 8) Including JSTL in JSF Advanced Web Technologies 8) Including JSTL in JSF Overview and Installation: Installing JSTL Overview and Installation: Installing JSTL 5 6 Problems with JSTL 1.0 and the JSP Iteration Tags 2 Expression Language ◮ The JSTL tag attributes are not defined with rtexprvalue="true" • So JSTL can parse EL, even in non-JSP-2 environments ◮ In JSP 2, the EL is evaluated before tags • So EL in JSTL attributes would be evaluated by JSP 2, not by JSTL, which is an illegal rtexprvalue ◮ So, you must disable the regular EL • Use web.xml declaration that refers to servlets 2.3; Disables the regular EL in the entire Web app • Use jsp-property-group in web.xml ; Disables the regular EL in selected pages • Use <%@ page isELIgnored="true" %> ; Disables the regular EL in current page Advanced Web Technologies 8) Including JSTL in JSF Advanced Web Technologies 8) Including JSTL in JSF Overview and Installation: Installing JSTL Iteration Tags 7 8
Looping Tags: Summary Looping Tags: Motivation ◮ JSP without JSTL Looping with explicit numeric values ◮ < UL > < % < c:forEach var=”name” begin=”x” end=”y” step=”z” > for(int i=0; i < messages.length; i++) { Blah, blah < c:out value=”$ { name } ”/ > String message = messages[i]; < /c:forEach > % > < LI >< %= message % > ◮ Looping over data structures < % } % > • Can loop down arrays, strings, collections, maps < /UL > < c:forEach var=”name” ◮ JSP with JSTL items=”array − or − collection” > < UL > Blah, blah < c:out value=”$ { name } ”/ > < c:forEach var=”message” items=”$ { messages } ” > < /c:forEach > < LI > < c:out value=”$ { message } ”/ > < /LI > Looping down delimited strings ◮ < /c:forEach > • forTokens < /UL > Advanced Web Technologies 8) Including JSTL in JSF Advanced Web Technologies 8) Including JSTL in JSF Iteration Tags Iteration Tags 9 10 Looping with Simple Numeric Values Looping with a Designated Step Size < %@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”% > < %@ taglib prefix=”c” < UL > uri=”http://java.sun.com/jsp/jstl/core”% > < c:forEach < UL > var=”seconds” < c:forEach var=”i” begin=”1” end=”10” > begin=”0” < LI >< c:out value=”$ { i } ”/ > end=”$ { pageContext.session.maxInactiveInterval } ” < /c:forEach > step=”100” > < /UL > < LI >< c:out value=”$ { seconds } ”/ > seconds. < /c:forEach > < LI > Timeout exceeded. < /UL > Advanced Web Technologies 8) Including JSTL in JSF Advanced Web Technologies 8) Including JSTL in JSF Iteration Tags Iteration Tags 11 12
Looping Down Arrays Looping Down Comma-Delimited Strings < % String[] words = { ”foo”, ”bar”, ”baz” } ; pageContext.setAttribute(”words”, words); % > < %@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”% > < %@ taglib prefix=”c” < H2 > Key Words: < /H2 > uri=”http://java.sun.com/jsp/jstl/core”% > < UL > < UL > < c:forEach var=”word” items=”$ { words } ” > < c:forEach < LI >< c:out value=”$ { word } ”/ > var=”country” < /c:forEach > items=”Australia,Canada,Japan,Philippines,USA” > < /UL > < LI >< c:out value=”$ { country } ”/ > < H2 > Values of the test Parameter: < /H2 > < /c:forEach > < UL > < /UL > < c:forEach var=”val” items=”$ { paramValues.test } ” > < LI >< c:out value=”$ { val } ”/ > < /c:forEach > < /UL > Advanced Web Technologies 8) Including JSTL in JSF Advanced Web Technologies 8) Including JSTL in JSF Iteration Tags Iteration Tags 13 14 Looping Down Arbitrarily-Delimited Logic Tags Strings < %@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” % > < UL > < c:forTokens var=”color” items=”(red (orange) yellow)(green)((blue) violet)” delims=”()” > < LI >< c:out value=”$ { color } ”/ > < /c:forTokens > < /UL > ◮ Point: forTokens built on forEach: you can build your own custom tags based on JSTL tags Advanced Web Technologies 8) Including JSTL in JSF Advanced Web Technologies 8) Including JSTL in JSF Iteration Tags Logic Tags 15 16
Conditional Evaluation Tags The ” if ” Tag ◮ One choice: if < c:if test=”$ { someTest } ” > < %@ taglib prefix=”c” Content uri=”http://java.sun.com/jsp/jstl/core”% > < /c:if > < UL > ◮ Lots of choices: choose < c:forEach var=”i” begin=”1” end=”10” > < LI >< c:out value=”$ { i } ”/ > < c:choose > < c:if test=”$ { i > 7 } ” > < c:when test=”test1” > Content1 < /c:when > (greater than 7) < c:when test=”test2” > Content2 < /c:when > < /c:if > ... < /c:forEach > < c:when test=”testN” > ContentN < /c:when > < /UL > < c:otherwise > Default Content < /c:otherwise > < /c:choose > ◮ Caution: resist use of business logic! Advanced Web Technologies 8) Including JSTL in JSF Advanced Web Technologies 8) Including JSTL in JSF Logic Tags Logic Tags: The “if” Tag 17 18 The ”choose” Tag URL-Handling Tags < %@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”% > ◮ <c:import> < UL > • Read content from arbitrary URLs < c:forEach var=”i” begin=”1” end=”10” > • Insert into page < LI >< c:out value=”$ { i } ”/ > • Store in variable < c:choose > • Or make accessible via a reader < c:when test=”$ { i < 4 } ” > (small) • Unlike <jsp:include> , not restricted to own system < /c:when > ◮ <c:redirect> < c:when test=”$ { i < 8 } ” > (medium) • Redirects response to specified URL < /c:when > ◮ <c:param> < c:otherwise > (large) • Encodes a request parameter and adds it to a URL < /c:otherwise > • May be used within body of <c:import> or <c:redirect> < /c:choose > < /c:forEach > < /UL > Advanced Web Technologies 8) Including JSTL in JSF Advanced Web Technologies 8) Including JSTL in JSF Logic Tags: The “choose” Tag Other Tags 19 20
Recommend
More recommend