JavaServer Pages Standard Tag Library (JSPTL)

Tag Libraries are a recent addition to JSP and have been quickly adopted by the development community. As with all new technologies, Tag Libraries are rapidly developing and changing.

As you saw in the section on cooperating tags, it is easy to write generic tags that provide useful functionality, such as iteration. Many Tag Libraries provided with JSP-compliant Web servers include their own custom tags, many of which are generic tags.

The proliferation of different Tag Libraries providing similar features has been seen as a problem, and the Java Community Process (JCP) is working on a standard set of tags to be included in the JSP standard.

The JavaServer Pages Standard Tag Library (JSPTL) is Java Community Process initiative and is specified on the JCP Web page at http://jcp.org/jsr/detail/052.jsp.

The latest implementation of the JSPTL can be downloaded from the Apache Jakarta TagLibs page at http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html.

The Jakarta project also provides a large number of other tag libraries addressing common page requirements, such as support for JNDI, JMS, and JDBC.

Using the JSPTL with the J2EE RI

Using the JSPTL libraries with the J2EE is a simple process. You start by downloading the JSPTL archive from the Apache Jakarta Web site at http://jakarta.apache.org/builds/jakarta-taglibs/releases/standard/.

Extract the downloaded archive and in the JSPTL directory, you will see a number of files, including two TLD files and some JAR files.

JSPTL defines two versions of the Tag Libraries. One version uses standard Java request time expressions, and the other uses a suggested scripting language. The two supplied TLD files refer to the two libraries known as jr (runtime expressions in file jsptl-tr.tld) and jx (scripting language in file jsptl-jx.tld).

In this section, you will use the JSPTL jr library to examine some of the features of the standard tags. You will need to include the JSPTL JAR file and TLD in your application. Look in the JSPTL directory for the jsptl.jar file (it may have a version number, such as jsptil-1.2.jar, or it may be an early access version jsptlea.jar).

You are now ready to include the JSPTL in your Web application. Start up deploytool and, to keep the demonstration simple, create a new application called jsptl. Create a new Web application and add the JSPTL JAR file (jsptl.jar) and the jr TLD file (jsptl-tr.tld). Figure 14.6 shows the file list from the Web Application Wizard (the JAR file has been added to the WEB-INF/lib directory).

Figure 14.6. Adding the JSPTL files.


Click on the Next button and on the Component Type page select the No Component option. Click on Next six times to get to the File Refs page and then add an entry for the TLD file. Figure 14.7 shows the entry mapping the logical name /jsptl-jr onto the actual file /WEB-INF/jsptl-jr.tld.

Figure 14.7. Defining the JSPTL TLD file reference.


Finally, click Finish. The jsptl application now includes the JSPTL Tag Libraries, and you can create new Web Applications that use this library.

Using the JSPTL forEach Tag

The JSPTL forEach tag performs a similar function to the forEach tag you developed today as part of the case study. However, the JSPTL iteration tag supports many different Java idioms for defining lists of values:

  • Arrays of Java objects

  • Arrays of Java primitives (the individual values are automatically wrapped an the appropriate wrapper object)

  • Objects in a java.util.Collection, such as list and sets

  • Objects in a java.util.Map collection

  • Objects defined by a java.util.Iterator

  • Objects defined by a java.util.Enumerator

  • Objects defined by a java.sql.ResultSet

  • Comma-separated values in a String, such as "apple,orange,pear"

The forEach tag defines a scripting variable to hold the value for the current loop iteration. You specify the name of this variable with the var attribute. The actual list of values is defined by the items attribute.

Listing 14.20 shows a simple example that displays a list of fruits defined in a comma-separated string.

Listing 14.20. Full Text of foreach.jsp
1: <%@ taglib uri="/jsptl-jr" prefix="jr" %>
2: <HTML><HEAD><TITLE>JSPTL foreach example</TITLE>
3: </HEAD>
4: <BODY>
5: <H1>JSPTL For Each Example</H1>
6: <jr:forEach var="fruit" items='"apple,orange,pear"'>
7:     <%=pageContext.getAttribute("fruit")%><BR>
8: </jr:forEach>
9: </BODY></HTML>

The Java string literal is defined as a tag attribute. The attribute is quoted in single quotes, and the string literal is defined in double quotes. The loop iteration variable is stored in the page context and has to be retrieved using the PageContext.getAttribute() method, as shown at line 7 of Listing 14.20.

Using the same forEach tag, but this time defining the fruits in an array of String objects, is shown in the following code fragment:

<% String[] fruits = {"apple","orange","pear"}; %>
<jr:forEach var="fruit" items="<%=fruits%>">
    <%=pageContext.getAttribute("fruit")%><BR>
</jr:forEach>

You can incorporate the JSPTL forEach tag into the case study example. Listing 14.21 shows a simple page to display a list of customers in the Agency database.

Listing 14.21. Full Text of listcust.jsp
 1: <%@ taglib uri="/jsptl-jr" prefix="jr" %>
 2: <HTML><HEAD><TITLE>JSPTL foreach customer</TITLE>
 3: </HEAD>
 4: <BODY>
 5: <jsp:useBean id="agency" class="web.AgencyBean" scope="request" />
 6: <H1><jsp:getProperty name="agency" property="agencyName"/></H1>
 7: <P>
 8: <jr:forEach var="cust" items="<%=agency.findAllCustomers()%>">
 9:     <%=pageContext.getAttribute("cust")%><BR>
10: </jr:forEach>
11: </BODY></HTML>

To deploy the example in Listing 14.21, you will need to add the Agency Session bean class files to your application. Figure 14.8 shows the files added to the jsptl application, which are agency.AgencyHome, agency.Agency, agency.NotFoundException, agency.DuplicateException, and web.AgencyBean.

Figure 14.8. The Agency class files used by listcust.jsp.


Other JSPTL Tags

The JSPTL library defines other structural tags. Tags are available to support tokens defined in a String, an if construct, and a case statement. Each of these is shown briefly in this section.

The forTokens tag iterates over a delimited list of values specified in a String. The delims attribute defines the delimiter, for example

<jr:forTokens var="token" items="apple|orange|pear" delims="|">
  ...
</jr:forTokens>

Simple optional inclusion of a tag body is supported by the if tag that defines a test attribute, for example

<jr:if test='<%((String)pageContext.getAttribute("cust")).equals("george")%>'>
  ...
</jr:if>

A case statement is supported by the choose tag and the nested when and otherwise tags. The choose tag structure is as follows:

<jr:choose>
  <when test="...">
    ...
  </when>
  <when test="...">
    ...
  </when>
    ...
  <otherwise>
    ...
  </otherwise>
</jr:choose>

As you can see, the JSPTL provides the basic structural elements necessary to add programming language, such as constructs to your Web pages.

JSPTL Scripting Language

The support for a scripting language in the JSPTL is very experimental and in the early stages of development. The intention behind the scripting language is to incorporate a powerful technique for adding programming capabilities to tags.

At the time of writing (late 2001), the JSPTL specification provides for pluggable scripting languages. A simple language is defined in the specification, but it should be possible to add other scripting languages, such as JavaScript or Perl.

The following example shows a forEach tag that iterates over the values defined by a scripting variable called customers.

<jx:forEach var="customer" items="$customers">
  <jx:expr value="$customer"/><br>
</jx:forEach>

In this example, the Tag Library prefix is jx rather than jr. The scripting variables are identified by a dollar sign ($). The expr tag is used to evaluate an expression in the scripting language and writes the results to the Web page (much like the request time <%= > tag). Other tags are defined for declaring scripting variables and setting the values of the scripting variables.

Full documentation for the latest developments in the scripting language support are available from the JCP and Apache Jakarta Web pages already mentioned.

Other Jakarta Tag Libraries

The Apache Jakarta open source project is developing a large number of Tag Libraries. Many of these libraries will become the de facto standard for JSP pages.

Already available are libraries for supporting

  • Time and Date

  • Database access

  • I/O support for protocols, such as HTTP, HTTPS, FTP, XML-RPC, and SOAP (SOAP is discussed further on Day 20, “Using RPC-Style Web Services with J2EE,” and Day 21, “Web Service Registries and Message-Style Web Services”)

  • JNDI

  • Support for HTTP session data

  • Regular expressions

  • XSL (XML style sheets that are discussed on Day 17, “Transforming XML Documents”)

New Tag Libraries are being developed and added to this collection as a need for them is identified. The Apache Jakarta Web site is the place to look for standard Tag Libraries that may implement the functionality you require.

http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.164.75