JSF and JSTL
JSF and JSTL
JSF and JSTL
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.
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
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
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
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
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.
Bean
<h:outputText value="#{cust.withdrawals}" escape="true"/>
15
18
19
20
22
24
26
29
30
Point:
forTokens built on forEach: you can build your own custom tags based on JSTL tags
31
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
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
Solution
Use JSTL
37
38
39
40
41
43
44
45
46
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.
49
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.
This fails
<c:forEach var="name" items="names"> <LI><h:outputText value="#{name}"/> </c:forEach>
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.
<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.
55
56
57
58
59
URL
jdbc:odbc:Northwind
Local access only; for testing. Not for serious applications.
Username
Empty string
Password
Empty string
60
sql:setDataSource
You can set a data source globally via configuration settings or application-scoped variables.
Preferred approach in real applications
61
sql:query
Form 1: use the sql attribute
<sql:query var="results" sql="SELECT * FROM "/>
Options
dataSource maxRows startRow
Caution
Embedding SQL directly in JSP may be hard to maintain.
62
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
64
65
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
<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
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:param>
69
XPath
<x:if> <x:choose> <x:when> <x:otherwise> <x:forEach>
XSLT
<x:transform> <x:param>
70
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
Looping tags
Explicit numeric values Arrays, collections, maps, strings, etc.
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
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.