2008 Marty Hall
Using the JSP Standard Tag Library (JSTL) with JSF
Originals of Slides and Source Code for Examples: http://www.coreservlets.com/JSF-Tutorial/
Customized Java EE Training: http://courses.coreservlets.com/
2
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Java 5 or 6, etc. Spring/Hibernate coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.
2008 Marty Hall
For live JSF training, please see training courses at http://courses.coreservlets.com/.
Taught by the author of Core Servlets and JSP, More Servlets and JSP, and this tutorial. Available at public venues, or customized versions can be held on-site at your organization.
Courses developed and taught by Marty Hall
Customized Java Training: http://courses.coreservlets.com/ Courses developed and taught by EE coreservlets.com experts (edited by Marty)
4
Java 5, Java 6, intermediate/beginning servlets/JSP, advanced servlets/JSP, Struts, JSF, Ajax, GWT, custom courses.
Spring, Hibernate, EJB3, Ruby/Rails Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Java 5 or 6, etc. Spring/Hibernate coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location. Contact [email protected] for details
Agenda
Obtaining JSTL documentation and code The JSTL Expression Language Looping Tags
Looping a certain number of times Looping over data structures Improving Ajax MVC data-handling examples
Conditional Evaluation Tags
Single choice Multiple choices
Database Access Tags Other Tags
6
Java EE training: http://courses.coreservlets.com
2008 Marty Hall
Overview and Installation
Customized Java EE Training: http://courses.coreservlets.com/
7
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Java 5 or 6, etc. Spring/Hibernate coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.
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
8
Java EE training: http://courses.coreservlets.com
Downloading JSTL
JSTL 1.1
Home page
http://jakarta.apache.org/taglibs/doc/ standard-doc/intro.html
Download
http://jakarta.apache.org/site/downloads/ downloads_taglibs-standard.cgi
Documentation (Javadoc)
http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/
Note
Many server packages (e.g., MyFaces) and IDEs (e.g., MyEclipse) already have JSTL 1.1 bundled
JSTL 1.2
As of 2/2008, only available as part of Java EE 5
Not as a separate download for use in other servers
9
Java EE training: http://courses.coreservlets.com
Enabling JSTL with MyEclipse
Using bundled JSTL version
File New Web Project, select JSTL 1.1 option
Using latest JSTL version
Download from Apache site Drag jstl.jar and standard.jar onto WEB-INF/lib
They get listed in "Referenced Libraries" I usually rename JAR files to show exact version
10
They show up here. But you drop them here.
Java EE training: http://courses.coreservlets.com
Enabling JSTL Manually
Put in Web App
Drop JAR files in the WEB-INF/lib folder of your Web app
Put in CLASSPATH
Add the JAR files to the CLASSPATH used by your development environment Do not add to the CLASSPATH used by the server
11
Java EE training: http://courses.coreservlets.com
The JSTL Expression Language
Accessed via ${expression} Similar to JavaScript and XPath Provides shorthand notation to access:
Attributes of standard servlet objects Bean properties Map, List, and Array elements
Is standard part of JSP 2.0 and 2.1
In JSTL, EL can be used only in attributes of JSTL tags In JSP 2.0, the EL can be used anywhere
web.xml element and page directive attribute let you disable the EL for backward compatibility
Covered in separate lecture
12
Java EE training: http://courses.coreservlets.com
2008 Marty Hall
Iteration Tags
Customized Java EE Training: http://courses.coreservlets.com/
14
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Java 5 or 6, etc. Spring/Hibernate coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.
JSF Constructs for Handling Variable-Length Data
Simplest for Page Author
Bean
<h:outputText value="#{cust.withdrawals}" escape="true"/>
Custom tag (non-looping)
<mytags:withDrawalTable customer="${cust}" border="..." headerStyles="..." .../>
15
Most Control for Page Author
h:dataTable t:dataList JSTL loop JSP scripting loop
Java EE training: http://courses.coreservlets.com
Looping Tags: Summary
Looping with explicit numeric values
<c:forEach var="name" begin="x" end="y" step="z"> Blah, blah ${name} </c:forEach>
Looping over data structures
Can loop down arrays, strings, collections, maps
<c:forEach var="name" items="array-or-collection"> Blah, blah ${name} </c:forEach>
Looping down delimited strings
forTokens
17
Java EE training: http://courses.coreservlets.com
Looping Tags: Motivation
JSP without JSTL
<UL> <% for(int i=0; i<messages.length; i++) { String message = messages[i]; %> <LI><%= message %> <% } %> </UL>
JSP with JSTL
<UL> <c:forEach var="message" items="${messages}"> <LI>${message} </c:forEach> </UL>
Java EE training: http://courses.coreservlets.com
18
Looping with Simple Numeric Values
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <UL> <c:forEach var="i" begin="1" end="10"> <LI>${i} </c:forEach> </UL>
19
Java EE training: http://courses.coreservlets.com
Looping with a Designated Step Size
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <UL> <c:forEach var="seconds" begin="0" end="${pageContext.session.maxInactiveInterval}" step="100"> <LI>${seconds} seconds. </c:forEach> <LI>Timeout exceeded. </UL>
20
Java EE training: http://courses.coreservlets.com
Looping Down Arrays (Servlet)
public class ArrayServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] words = { "foo", "bar", "baz"}; request.setAttribute("words", words); String address = "/WEB-INF/results/array-loop.jsp"; RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); } }
Note url-pattern in web.xml is /test-array
21
Java EE training: http://courses.coreservlets.com
Looping Down Arrays
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <H2>Key Words:</H2> <UL> <c:forEach var="word" items="${words}"> <LI>${word} </c:forEach> </UL> <H2>Values of the test Parameter:</H2> <UL> <c:forEach var="val" items="${paramValues.test}"> <LI>${val} </c:forEach> </UL> Java EE training: http://courses.coreservlets.com
22
Note for Older Servers
Examples in this lecture assume JSP 2.0+
JSTL also runs in older servers, but you have to replace direct output of ${foo} with <c:out value="$foo"/>
Previous example (JSP 2.0+)
<UL> <c:forEach var="word" items="${words}"> <LI>${word} </c:forEach> </UL>
Previous example (JSP 1.2)
<UL> <c:forEach var="word" items="${words}"> <LI><c:out value="${word}"/> </c:forEach> </UL>
23
Java EE training: http://courses.coreservlets.com
${foo} vs. <c:out value="${foo}"/>
c:out runs on older servers
Oracle 9i, BEA WebLogic 8.x, IBM WebSphere 5.x
c:out escapes HTML (XML) characters
<c:out value="<h1>"/> outputs <h1>
Very important if value being output comes from end user. Disable with <c:out value="" escapeXml="false"/>
c:out lets you supply a default value
<c:out value="${foo}" default="explicit value"/> or <c:out value="${foo}" default="${calculatedValue}"/> The default is output when ${foo} evaluates to null
24
Java EE training: http://courses.coreservlets.com
Looping Down Collections: Notes
Can loop down collections other than arrays
"items" can refer to an array, collection (List, Map, Set, etc.), comma-separated String, Iterator, or Enumeration
Can access sub-elements of local variable
<c:forEach var="bean" items="${collectionOfBeans}"> ${bean.property} </c:forEach> <c:forEach var="collection" items="${collectionOfCollections}"> ${collection[part]} </c:forEach> For details, see separate lecture on the JSP expression language
25
Java EE training: http://courses.coreservlets.com
Looping Down Arrays: Accessing SubElements (Servlet)
public class ArrayServlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Name[] names = { new Name("Bill", "Gates"), new Name("Larry", "Ellison"), new Name("Sam", "Palmisano"), new Name("Scott", "McNealy"), new Name("Eric", "Schmidt"), new Name("Jeff", "Bezos") }; request.setAttribute("names", names); String[][] sales = { {"2005", "12,459", "15,622"}, {"2006", "18,123", "17,789"}, {"2007", "21,444", "23,555"} }; request.setAttribute("sales", sales); String address = "/WEB-INF/results/array-loop2.jsp"; RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); } Note: url-pattern is /test-array2 } Java EE training: http://courses.coreservlets.com
26
Looping Down Arrays: Accessing SubElements (Bean)
public class Name { private String firstName; private String lastName; public Name(String firstName, String lastName) { setFirstName(firstName); setLastName(lastName); } public String getFirstName() { return(firstName); } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return(lastName); } public void setLastName(String lastName) { this.lastName = lastName; } }
27
Java EE training: http://courses.coreservlets.com
Looping Down Arrays: Accessing SubElements (JSP)
<H2>Attendees at the coreservlets.com Party</H2> <UL> <c:forEach var="name" items="${names}"> <LI>${name.firstName} ${name.lastName} </c:forEach> </UL> <H2>Comparing Apples and Oranges</H2> <TABLE BORDER="1"> <TR><TH>Year</TH> <TH>Apples Sold</TH> <TH>Oranges Sold</TH></TR> <c:forEach var="row" items="${sales}"> <TR><c:forEach var="col" items="${row}"> <TD>${col}</TD> </c:forEach> </c:forEach> </TABLE>
28
Java EE training: http://courses.coreservlets.com
Looping Down Arrays: Accessing SubElements (Result)
29
Java EE training: http://courses.coreservlets.com
Looping Down CommaDelimited Strings
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <UL> <c:forEach var="country" items="Australia,Canada,Japan,Philippines,Mexico,USA"> <LI>${country} </c:forEach> </UL>
30
Java EE training: http://courses.coreservlets.com
Looping Down ArbitrarilyDelimited 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>${color} </c:forTokens> </UL>
Point:
forTokens built on forEach: you can build your own custom tags based on JSTL tags
31
Java EE training: http://courses.coreservlets.com
2008 Marty Hall
Looping Tags in Action: Ajax Data Handling
Customized Java EE Training: http://courses.coreservlets.com/
32
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Java 5 or 6, etc. Spring/Hibernate coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Original Version
In "Ajax Data Handling" showed, showed example that displayed lists of cities
JavaScript
Sent out request to server Took data back in XML, JSON, or string format Extracted information and produced HTML table Inserted table into page
HTML
Loaded JavaScript files Had button that trigged the action
Servlet
Produced List of City objects
JSP
Put city info into XML, JSON, or string format
Repeated entries for each city in List
Notes and source code for original example
http://courses.coreservlets.com/Course-Materials/ajax.html
33
Java EE training: http://courses.coreservlets.com
HTML Code
... <fieldset> <legend>Getting XML Data from Server...</legend> <form action="#"> <label for="city-type-1">City Type:</label> <select id="city-type-1"> <option value="top-5-cities"> Largest Five US Cities</option> <option value="second-5-cities"> Second Five US Cities</option> ... </select> <br/> <input type="button" value="Show Cities" onclick='xmlCityTable("city-type-1", "xml-city-table")'/> </form> <p/> <div id="xml-city-table"></div> Java EE training: http://courses.coreservlets.com </fieldset>...
34
Servlet Code
public class ShowCities extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); String cityType = request.getParameter("cityType"); List<City> cities = findCities(cityType); request.setAttribute("cities", cities); String format = request.getParameter("format"); String outputPage; if ("xml".equals(format)) { response.setContentType("text/xml"); outputPage = "/WEB-INF/results/cities-xml.jsp"; } ... RequestDispatcher dispatcher = request.getRequestDispatcher(outputPage); dispatcher.include(request, response); }
35
Java EE training: http://courses.coreservlets.com
Original JSP Code (cities-xml-before-jstl.jsp)
<?xml version="1.0" encoding="UTF-8"?> <cities> <city> <name>${cities[0].name}</name> <time>${cities[0].shortTime}</time> <population>${cities[0].population}</population> </city> <city> <name>${cities[1].name}</name> <time>${cities[1].shortTime}</time> <population>${cities[1].population}</population> </city> ... <! Repeated for cities 2 and 3 --> <city> <name>${cities[4].name}</name> <time>${cities[4].shortTime}</time> <population>${cities[4].population}</population> </city> </cities>
36
Java EE training: http://courses.coreservlets.com
Problems with JSP Code
Repetitive
Almost-identical city entries repeated five times If the city format changes, all five entries need to change
Presumes exactly five cities
Will not work with a different number of cities
Solution
Use JSTL
37
Java EE training: http://courses.coreservlets.com
Updated JSP Code (cities-xml.jsp)
<?xml version="1.0" encoding="UTF-8"?> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <cities> <c:forEach var="city" items="${cities}"> <city> <name>${city.name}</name> <time>${city.shortTime}</time> <population>${city.population}</population> </city> </c:forEach> </cities>
38
Java EE training: http://courses.coreservlets.com
Alternative Version (Using XML Syntax: cities-xml.jspx)
<?xml version="1.0" encoding="UTF-8"?> <cities xmlns:c="http://java.sun.com/jsp/jstl/core"> <c:forEach var="city" items="${cities}"> <city> <name>${city.name}</name> <time>${city.shortTime}</time> <population>${city.population}</population> </city> </c:forEach> </cities>
39
Java EE training: http://courses.coreservlets.com
XML Data: Results
40
Java EE training: http://courses.coreservlets.com
JSON Version: Original (cities-json-before-jstl.jsp)
({ "names": ["${cities[0].name}", "${cities[1].name}", "${cities[2].name}", "${cities[3].name}", "${cities[4].name}"], "times": ["${cities[0].shortTime}", "${cities[1].shortTime}", "${cities[2].shortTime}", "${cities[3].shortTime}", "${cities[4].shortTime}"], "populations": ["${cities[0].population}", "${cities[1].population}", "${cities[2].population}", "${cities[3].population}", "${cities[4].population}"] })
Java EE training: http://courses.coreservlets.com
41
JSON Version: Updated (cities-json.jsp)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> ({ "names": [<c:forEach var="city" items="${cities}"> "${city.name}", </c:forEach>], "times": [<c:forEach var="city" items="${cities}"> "${city.shortTime}", </c:forEach>], "populations": [<c:forEach var="city" items="${cities}"> "${city.population}", </c:forEach>] })
42
Java EE training: http://courses.coreservlets.com
JSON Version: Results
43
Java EE training: http://courses.coreservlets.com
Delimited String Version: Original (cities-string-before-jstl.jsp)
${cities[0].name}#${cities[1].name}#...#${cities[4].name} ${cities[0].shortTime}#...#${cities[4].shortTime} ${cities[0].population}#...#${cities[4].population}
44
Java EE training: http://courses.coreservlets.com
Delimited String Version: Updated (cities-string.jsp)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><c:forEach var="city" items="${cities}">${city.name}#</c:forEach> <c:forEach var="city" items="${cities}">${city.shortTime}#</c:forEach> <c:forEach var="city" items="${cities}">${city.population}#</c:forEach>
45
Java EE training: http://courses.coreservlets.com
String Version: Results
46
Java EE training: http://courses.coreservlets.com
2008 Marty Hall
Logic Tags
Customized Java EE Training: http://courses.coreservlets.com/
47
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Java 5 or 6, etc. Spring/Hibernate coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Conditional Evaluation Tags
One choice: if
<c:if test="${someTest}"> Content </c:if>
Lots of choices: choose
<c:choose> <c:when test="test1">Content1</c:when> <c:when test="test2">Content2</c:when> ... <c:when test="testN">ContentN</c:when> <c:otherwise>Default Content</c:otherwise> </c:choose>
Caution: resist use of business logic!
48
Java EE training: http://courses.coreservlets.com
The "if" Tag
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <UL> <c:forEach var="i" begin="1" end="10"> <LI>${i} <c:if test="${i > 7}"> (greater than 7) </c:if> </c:forEach> </UL>
49
Java EE training: http://courses.coreservlets.com
The "choose" Tag
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <UL> <c:forEach var="i" begin="1" end="10"> <LI>${i} <c:choose> <c:when test="${i < 4}"> (small) </c:when> <c:when test="${i < 8}"> (medium) </c:when> <c:otherwise> (large) </c:otherwise> </c:choose> </c:forEach> </UL>
50
Java EE training: http://courses.coreservlets.com
2008 Marty Hall
Using JSTL in JSF Pages
Customized Java EE Training: http://courses.coreservlets.com/
51
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Java 5 or 6, etc. Spring/Hibernate coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Output Pages vs. Direct-Access Pages
Output pages
In pages that are the result of a navigation rule (i.e., listed under to-view-id), you can use JSTL as shown on previous slides
Since bean is already in request, session, or application
Direct access pages
JSTL does not know about faces-config.xml, so you cannot refer to beans that are listed there but not yet instantiated
Using direct-access pages is rare in real life, but is very useful for testing when learning JSTL Solution: use h:outputText to refer to bean once first
Declare bean containing array of numbers in faces-config.xml First num: <h:outputText value="#{myBean.numbers[0]}/> <c:forEach var="num" items="${myBean.numbers}>...
52
Java EE training: http://courses.coreservlets.com
JSF Does Not Know About JSTL Variables
JSTL puts data in page scope This works
<c:forEach var="name" items="${names}"> <LI>${name} </c:forEach> Example assumes this is an output page and "names" set earlier by JSF
This fails
<c:forEach var="name" items="names"> <LI><h:outputText value="#{name}"/> </c:forEach>
Supposedly fixed in JSTL 1.2
But there are no standalone releases of JSTL 1.2 Use facelets instead
53
Java EE training: http://courses.coreservlets.com
2008 Marty Hall
Database Access Tags
Customized Java EE Training: http://courses.coreservlets.com/
54
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Java 5 or 6, etc. Spring/Hibernate coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Database Access Tags
<sql:setDataSource>
Specifies data source (can also be set by config settings)
<sql:query>
Queries database and stores ResultSet in variable Warning: this usage violates rule of keeping business logic out of presentation layer. Instead, do database access in servlet and pass results to JSP via MVC.
<sql:update> <sql:param> <sql:dateParam> <sql:transaction>
Performs the enclosed <sql:query> and <sql:update> actions as a single transaction
55
Java EE training: http://courses.coreservlets.com
Aside: The Microsoft Access Northwind Database
Database that comes preinstalled with Microsoft Office
56
Java EE training: http://courses.coreservlets.com
Using Microsoft Access via ODBC
Click Start, Settings, Control Panel, Administrative Tools, Data Sources, System DSN, and select Add
57
Java EE training: http://courses.coreservlets.com
Using Microsoft Access via ODBC (Continued)
Select Microsoft Access Driver, Finish, type a name under Data Source Name, and hit Select
58
Java EE training: http://courses.coreservlets.com
Using Microsoft Access via ODBC (Continued)
Navigate to the Samples directory of MS Office, select Northwind.mdb, hit OK, then hit OK in following two windows
59
Java EE training: http://courses.coreservlets.com
Using Microsoft Access via ODBC (Continued)
Driver
sun.jdbc.odbc.JdbcOdbcDriver
Comes bundled with JDK
URL
jdbc:odbc:Northwind
Local access only; for testing. Not for serious applications.
Username
Empty string
Password
Empty string
60
Java EE training: http://courses.coreservlets.com
sql:setDataSource
You can set a data source globally via configuration settings or application-scoped variables.
Preferred approach in real applications
Or, you can set it on a specific page
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %> <sql:setDataSource driver="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:Northwind" user="" password=""/>
61
Java EE training: http://courses.coreservlets.com
sql:query
Form 1: use the sql attribute
<sql:query var="results" sql="SELECT * FROM "/>
Form 2: put the query in the body of the tag
<sql:query var="results"> SELECT * FROM </sql:query>
Options
dataSource maxRows startRow
Caution
Embedding SQL directly in JSP may be hard to maintain.
62
Java EE training: http://courses.coreservlets.com
Simple Example
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %> <sql:setDataSource driver="sun.jdbc.odbc.JdbcOdbcDriver" url="jdbc:odbc:Northwind" user="" password=""/>
63
Java EE training: http://courses.coreservlets.com
Simple Example (Continued)
<sql:query var="employees"> SELECT * FROM employees </sql:query> <UL> <c:forEach var="row" items="${employees.rows}"> <LI>${row.firstname} ${row.lastname} </c:forEach> </UL>
64
Java EE training: http://courses.coreservlets.com
Simple Example: Results
65
Java EE training: http://courses.coreservlets.com
2008 Marty Hall
Other Tags
Customized Java EE Training: http://courses.coreservlets.com/
66
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Java 5 or 6, etc. Spring/Hibernate coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.
URL-Handling Tags
<c:import>
Read content from arbitrary URLs
Insert into page Store in variable Or make accessible via a reader
Unlike <jsp:include>, not restricted to own system
<c:redirect>
Redirects response to specified URL
<c:param>
Encodes a request parameter and adds it to a URL May be used within body of <c:import> or <c:redirect>
67
Java EE training: http://courses.coreservlets.com
Formatting Tags
<fmt:formatNumber>
Formats a numeric value as a number, currency value, or percent, in a locale-specific manner
<fmt:parseNumber>
Reads string representations of number, currency value, or percent
68
<fmt:formatDate> <fmt:parseDate> <fmt:timeZone> <fmt:setTimeZone>
Java EE training: http://courses.coreservlets.com
Internationalization (I18N) Tags
<fmt:setLocale>
Sets Locale
<fmt:setBundle> <fmt:bundle> <fmt:message>
Retrieves localized message from a resource bundle
<fmt:param>
69
Java EE training: http://courses.coreservlets.com
XML Manipulation Tags
Core
<x:parse>
XPath
<x:if> <x:choose> <x:when> <x:otherwise> <x:forEach>
XSLT
<x:transform> <x:param>
70
Java EE training: http://courses.coreservlets.com
Summary
JSTL is similar to the old Struts looping and logic tags, but better JSTL is standardized, but not a standard part of JSP
It is supposed to be included with all JSF implementations
Supports a concise expression language
Lets you access bean properties and implicit objects EL is standard part of JSP 2.0, JSP 2.1, and JSF
Looping tags
Explicit numeric values Arrays, collections, maps, strings, etc.
Conditional evaluation tags
Single options Multiple options
71
Java EE training: http://courses.coreservlets.com
Summary (Continued)
Database Access Tags
Always a bad idea when using MVC approach
With or without a framework like JSF or Struts
sql:setDataSource to specify a database sql:query to perform query Loop down results using iteration tags
Other tags
72
Handling URLS Internationalizing pages Formatting strings Manipulating XML
Java EE training: http://courses.coreservlets.com
2008 Marty Hall
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
73
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Java 5 or 6, etc. Spring/Hibernate coming soon. Developed and taught by well-known author and developer. At public venues or onsite at your location.