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, andattr1 ... attrN
are attributes that modify the behavior of the tag.To use a custom tag in a JSP page, you must
- Declare the tag library containing the tag
- Make the tag library implementation available to the web application
See Chapter 15 for detailed information on the different types of tags and how to implement tags.
Declaring Tag Libraries
To declare that a JSP page will use tags defined in a tag library, you include a
taglib
directive in the page before any custom tag from that tag library is used. If you forget to include thetaglib
directive for a tag library in a JSP page, the JSP compiler will treat any invocation of a custom tag from that library as static data and will 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/
. A translation error will occur if the value points to a directory that doesn't exist or if it is used in conjunction with theuri
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 theWEB-INF
directory or subdirectory of the WAR file or in theMETA-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 file name:<%@ 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 sampleiterator.war
is provided in<
INSTALL
>/j2eetutorial14/examples/web/provided-wars/
. To build the example, follow these steps:
- In a terminal window, go to
<
INSTALL
>/j2eetutorial14/examples/web/iterator/
.- Run
asant
build
. This target will spawn any necessary compilations and will copy files to the<
INSTALL
>/j2eetutorial14/examples/web/iterator/build/
directory.To package and deploy the example using
asant
, follow these steps:
- Run
asant
create-war
.- Run
asant
deploy-war
.To learn how to configure the example, use
deploytool
to package and deploy it:
- Start
deploytool
.- Create a web application called
iterator
by running the New Web Component wizard. Select FileRight ArrowNewRight ArrowWeb Component.- In the New Web Component wizard:
- Select the Create New Stand-Alone WAR Module radio button.
- In the WAR File field, enter
<
INSTALL
>/docs/tutorial/examples/web/iterator/iterator.war
. The WAR Display Name field showsiterator
.- In the Context Root field, enter
/iterator
.- Click Edit Contents.
- In the Edit Contents dialog box, navigate to
<
INSTALL
>/docs/tutorial/examples/web/iterator/build/
. Select theindex.jsp
andlist.jsp
JSP pages anditerator.tld
and click Add. Notice thatiterator.tld
is put into/WEB-INF/
. Click OK.- Click Next.
- Select the No Component radio button, then click Next.
- Click Finish.
You map a logical name to an absolute location in the web application deployment descriptor. For the iterator example, map the logical name
/tlt
to the absolute location/WEB-INF/iterator.tld
usingdeploytool
by following these steps:
- Select the File Ref's tab.
- Click the Add Tag Library button in the JSP Tag Libraries tab.
- Enter the relative URI
/tlt
in the URI column.- Enter the absolute location
/WEB-INF/iterator.tld
in the Location column.You can also reference a TLD in a
taglib
directive by using an absolute URI. For example, the absolute URIs for the JSTL library 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
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 thetaglib
element toweb.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 must 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 can load a tag library into all the web applications running on the server. For example, in the Application Server, the JSTL TLDs and libraries are distributed in the archiveappserv-jstl.jar
in<
J2EE_HOME
>/lib/
. 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.To package the
iterator
tag library implementation in the/WEB-INF/classes/
directory and deploy theiterator
example withdeploytool
, follow these steps:
- Select the General tab.
- Click Edit Contents.
- Add the iterator tag library classes.
- In the Edit Contents dialog box, navigate to
<
INSTALL
>/docs/tutorial/examples/web/iterator/build/
.- Select the
iterator
andmyorg
packages and click Add. Notice that the tag library implementation classes are packaged into/WEB-INF/classes/
.- Click OK.
- Select FileRight ArrowSave.
- Start the Application Server.
- Deploy the application.
- Select ToolsRight ArrowDeploy.
- Click OK.
To run the
iterator
application, open the URLhttp://localhost:8080/iterator
in a browser.