Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

Using Custom Tags

Custom tags are user-defined JSP language elements that encapsulate recurring tasks. Custom tags are distributed in a tag library, which defines a set of related custom tags and contains the objects that implement the tags.

Custom tags have the syntax

<prefix:tag attr1="value" ... attrN="value" />  

or

<prefix:tag attr1="value" ... attrN="value" > 
  body
</prefix:tag> 

where prefix distinguishes tags for a library, tag is the tag identifier, and attr1 ... attrN are attributes that modify the behavior of the tag.

To use a custom tag in a JSP page, you must:

See Chapter 16 for detailed information on the different types of tags and how to implement tags.

Declaring Tag Libraries

You declare that a JSP page will use tags defined in a tag library by including a taglib directive in the page before any custom tag from that tag library is used. If you forget to include the taglib directive for a tag library in a JSP page, the JSP compiler will treat any invocation of a custom tag from that library as template data, and simply insert the text of the custom tag call into the response.

<%@ taglib prefix="tt" [tagdir=/WEB-INF/tags/dir | uri=URI ] %> 

The prefix attribute defines the prefix that distinguishes tags defined by a given tag library from those provided by other tag libraries.

If the tag library is defined with tag files (see Encapsulating Reusable Content using Tag Files), you supply the tagdir attribute to identify the location of the files. The value of the attribute must start with /WEB-INF/tags/ and a translation error will occur if the value points to a directory that doesn't exist or if used in conjunction with the uri attribute.

The uri attribute refers to a URI that uniquely identifies the tag library descriptor (TLD), a document that describes the tag library (See Tag Library Descriptors).

Tag library descriptor file names must have the extension .tld. TLD files are stored in the WEB-INF directory or subdirectory of the WAR file or in the META-INF/ directory or subdirectory of a tag library packaged in a JAR. You can reference a TLD directly or indirectly.

The following taglib directive directly references a TLD filename:

<%@ taglib prefix="tlt" uri="/WEB-INF/iterator.tld"%> 

This taglib directive uses a short logical name to indirectly reference the TLD:

<%@ taglib prefix="tlt" uri="/tlt"%> 

The iterator example defines and uses a simple iteration tag. The JSP pages use a logical name to reference the TLD. A sample iterator.war is provided in <INSTALL>/jwstutorial13/examples/web/provided-wars/. To build the example:

  1. In a terminal window, go to <INSTALL>/jwstutorial13/examples/web/iterator/.
  2. Run ant build. This target will spawn any necessary compilations and copy files to the <INSTALL>/jwstutorial13/examples/web/iterator/build/ directory.

You map a logical name to an absolute location in the Web application deployment descriptor. The iterator example specifies the mapping of the logical name /tlt to the absolute location /WEB-INF/iterator.tld, with the following descriptor element:

<jsp-config>
  <taglib>
    <taglib-uri>/tlt</taglib-uri>
    <taglib-location>/WEB-INF/iterator.tld</taglib-location>
  </taglib>
</jsp-config> 

You can also reference a TLD in a taglib directive with an absolute URI. For example, the absolute URIs for the JSTL library are:

When you reference a tag library with an absolute URI that exactly matches the URI declared in the taglib element of the TLD (see Tag Library Descriptors), you do not have to add the taglib element to web.xml; the JSP container automatically locates the TLD inside the JSTL library implementation.

Including the Tag Library Implementation

In addition to declaring the tag library, you also need to make the tag library implementation available to the Web application. There are several ways to do this. Tag library implementations can be included in a WAR in an unpacked format: tag files are packaged in the /WEB-INF/tag/ directory and tag handler classes are packaged in the /WEB-INF/classes/ directory of the WAR. Tag libraries already packaged into a JAR file are included in the /WEB-INF/lib/ directory of the WAR. Finally, an application server may load a tag library into all the Web applications running on the server. For example, in the Java WSDP, the JSTL TLDs and libraries are distributed in the archives standard.jar and jstl.jar in <JWSDP_HOME>/jstl/lib/. If you copy these archives to the directory <JWSDP_HOME>/common/lib, they will automatically be loaded into the classpath of all Web applications running on Tomcat.

In the iterator example, the Ant build script compiles the iterator tag library implementation into the /WEB-INF/classes/ directory. To install the iterator example into Tomcat:

  1. Start Tomcat.
  2. Run ant install. The install target notifies Tomcat that the new context is available.

To run the iterator application, open the URL http://localhost:8080/iterator in a browser.

Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.