background image

Using Custom Tags

<< Retrieving JavaBeans Component Properties | Tag Library Descriptor >>
<< Retrieving JavaBeans Component Properties | Tag Library Descriptor >>

Using Custom Tags

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
Declare the tag library containing the tag
Make the tag library implementation available to the web application
See
Chapter 8, "Custom Tags in JSP Pages"
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 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 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"
on page 233
), 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 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" on page 247
).
Using Custom Tags
The Java EE 5 Tutorial · September 2007
172