Using JSTL - The Java EE 5 Tutorial (original) (raw)
2. Using the Tutorial Examples
3. Getting Started with Web Applications
5. JavaServer Pages Technology
7. JavaServer Pages Standard Tag Library
Flow Control Tags in the Core Tag Library
Internationalization Tag Library
Further Information about JSTL
10. JavaServer Faces Technology
11. Using JavaServer Faces Technology in JSP Pages
12. Developing with JavaServer Faces Technology
13. Creating Custom UI Components
14. Configuring JavaServer Faces Applications
15. Internationalizing and Localizing Web Applications
16. Building Web Services with JAX-WS
17. Binding between XML Schema and Java Classes
19. SOAP with Attachments API for Java
21. Getting Started with Enterprise Beans
23. A Message-Driven Bean Example
24. Introduction to the Java Persistence API
25. Persistence in the Web Tier
26. Persistence in the EJB Tier
27. The Java Persistence Query Language
28. Introduction to Security in the Java EE Platform
29. Securing Java EE Applications
31. The Java Message Service API
32. Java EE Examples Using the JMS API
36. The Coffee Break Application
37. The Duke's Bank Application
Using JSTL
JSTL includes a wide variety of tags that fit into discrete functional areas. To reflect this, as well as to give each area its own namespace, JSTL is exposed as multiple tag libraries. The URIs for the libraries are as follows:
- Core: http://java.sun.com/jsp/jstl/core
- XML: http://java.sun.com/jsp/jstl/xml
- Internationalization: http://java.sun.com/jsp/jstl/fmt
- SQL: http://java.sun.com/jsp/jstl/sql
- Functions: http://java.sun.com/jsp/jstl/functions
Table 7-1 summarizes these functional areas along with the prefixes used in this tutorial.
Table 7-1 JSTL Tags
Area | Subfunction | Prefix |
---|---|---|
Core | Variable support | c |
Flow control | ||
URL management | ||
Miscellaneous | ||
XML | Core | x |
Flow control | ||
Transformation | ||
I18N | Locale | fmt |
Message formatting | ||
Number and date formatting | ||
Database | SQL | sql |
Functions | Collection length | fn |
String manipulation |
Thus, the tutorial references the JSTL core tags in JSP pages by using the following taglib directive:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
In addition to declaring the tag libraries, tutorial examples access the JSTL API and implementation. In the Application Server, the JSTL TLDs and libraries are distributed in the archive as-install/lib/appserv-jstl.jar. This library is automatically loaded into the classpath of all web applications running on the Application Server, so you don’t need to add it to your web application.
Tag Collaboration
Tags usually collaborate with their environment in implicit and explicit ways. Implicit collaboration is done by means of a well-defined interface that allows nested tags to work seamlessly with the ancestor tag that exposes that interface. The JSTL conditional tags employ this mode of collaboration.
Explicit collaboration happens when a tag exposes information to its environment. JSTL tags expose information as JSP EL variables; the convention followed by JSTL is to use the name var for any tag attribute that exports information about the tag. For example, the forEach tag exposes the current item of the shopping cart it is iterating over in the following way:
<c:forEach var="item" items="${sessionScope.cart.items}"> ...
In situations where a tag exposes more than one piece of information, the name var is used for the primary piece of information being exported, and an appropriate name is selected for any other secondary piece of information exposed. For example, iteration status information is exported by the forEach tag through the attribute status.
When you want to use an EL variable exposed by a JSTL tag in an expression in the page’s scripting language (see Chapter 9, Scripting in JSP Pages), you use the standard JSP element jsp:useBean to declare a scripting variable.
For example, tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookshowcart.jsp removes a book from a shopping cart using a scriptlet. The ID of the book to be removed is passed as a request parameter. The value of the request parameter is first exposed as an EL variable (to be used later by the JSTL sql:query tag) and then is declared as a scripting variable and passed to the cart.remove method:
<c:set var="bookId" value="${param.Remove}"/> <jsp:useBean id="bookId" type="java.lang.String" /> <% cart.remove(bookId); %> <sql:query var="books" dataSource="${applicationScope.bookDS}"> select * from PUBLIC.books where id = ? <sql:param value="${bookId}" />
Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Legal Notices