Recipe3.1.Using JSTL


Recipe 3.1. Using JSTL

Problem

You want to use the JSP Standard Tag Library (JSTL) tags in your Struts application.

Solution

Download the Jakarta Taglibs JSTL reference implementation from http://jakarta.apache.org/taglibs. Copy the jstl.jar and standard.jar files from the lib folder into your applications WEB-INF/lib folder. Then copy the c.tld, fmt.tld, sql.tld, and x.tld files from the tlds folder into your applications WEB-INF/lib folder.

Use the appropriate taglib directives on JSP pages where you want to use JSTL:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

Table 3-1 lists the JSTL tag libraries and the corresponding URIs.

Table 3-1. JSTL tag library URIs

Tag library

JSTL 1.0 Taglib URI

JSTL 1.1 Taglib URI

Core

http://java.sun.com/jstl/core

http://java.sun.com/jsp/jstl/core

Formatting

http://java.sun.com/jstl/fmt

http://java.sun.com/jsp/jstl/fmt

SQL

http://java.sun.com/jstl/sql

http://java.sun.com/jsp/jstl/sql

XML

http://java.sun.com/jstl/xml

http://java.sun.com/jsp/jstl/xml

Functions

N/A

http://java.sun.com/jsp/jstl/functions


Discussion

JSTL is a powerful set of tag libraries that should be a part of any Struts developer's toolkit. JSTL contains tags for outputting JavaBean properties, looping, conditional logic, and URL formatting. There are tags for formatting and parsing dates and numbers. The XML tag library can be used to parse and process XML on a JSP page. The tags of the SQL tag library interact with a relational database. The Functions tag library provides useful functions that can be used in expressions, primarily for string manipulation.

By far the most important of these to have in your bag of developer tricks is the Core tag library. This library contains tags that can be used instead of many of the Struts bean logic tags. Why would you want to use these tags instead of the Struts tags? The answer is a practical one: These tags are more powerful and easier to use than the Struts tags. Make no mistake, however; the folks on the Struts project are not offended by this. Quite the contrary. JSTL has allowed Struts to focus on what it does best: providing the controller glue for robust JSP-based web applications.

Take a look at how you would implement a loop and display output using JSTL tags compared to the Struts tags. First, here's the Struts version:

<ul>     <logic:iterate  name="branch" property="customers">         <li>             <bean:write name="cust" property="lastName"/>,              <bean:write name="cust" property="firstName"/>         </li>     </logic:iterate> </ul>

In JSTL, this becomes a lot simpler:

<ul>     <c:forEach var="cust" items="${branch.customers}">         <li>             <c:out value="${cust.lastName}, ${cust.firstName}"/>         </li>     </c:forEach> </ul>

The cool part is that you don't have to choose one over the other. JSTL tags can be introduced into an application as you learn it. The JSP Expression Language (EL) enables easy access to data in ActionForms and objects available in the various JSP scopes (page, request, session, and application). The hardest decision you will have to make is not whether to use JSTL, but which version of JSTL to use. If you are using a JSP 2.0/Servlet 2.4 container such as Tomcat 5, you should use JSTL 1.1. Otherwise, you'll need to use JSTL 1.0.

Throughout this book, where appropriate, JSTL examples will be provided along with the pure Struts-based examples. In many cases, examples are provided that use the capabilities of both Struts and JSTL.

See Also

Recipe 3.2 shows how you can use EL expressions with the Struts tags. JavaServer Pages by Hans Bergsten (O'Reilly) covers JSTL in great detail and is an invaluable source. Sun provides an excellent tutorial on JSTL that can be found at http://java.sun.com/tutorials/jstl.

I've created a handy quick reference guide for JSTL. This guide can be found in PDF format at http://www.jadecove.com/jstl-quick-reference.pdf.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net