Tag Library Descriptors
If you want to redistribute your tag files or implement your custom tags with tag handlers written in Java, you must declare the tags in a tag library descriptor (TLD). A tag library descriptor is an XML document that contains information about a library as a whole and about each tag contained in the library. TLDs are used by a web container to validate the tags and by JSP page development tools.
Tag library descriptor file names must have the extension
.tldand must be packaged 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. If a tag is implemented as a tag file and is packaged in/WEB-INF/tags/or a subdirectory, a TLD will be generated automatically by the web container, though you can provide one if you wish.A TLD must begin with a root
taglibelement that specifies the schema and required JSP version:<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web- jsptaglibrary_2_0.xsd" version="2.0">Table 15-6 lists the subelements of the
taglibelement.
Table 15-6 taglib Subelements Element Description description (optional) A string describing the use of the tag library.display-name (optional) Name intended to be displayed by tools.icon (optional) Icon that can be used by tools.tlib-version The tag library's version.short-name (optional) Name that could be used by a JSP page-authoring tool to create names with a mnemonic value.uri A URI that uniquely identifies the tag library. validator See validator Element .listener See listener Element .tag-file | tag Declares the tag files or tags defined in the tag library. See Declaring Tag Files and Declaring Tag Handlers. A tag library is considered invalid if atag-fileelement has anamesubelement with the same content as anamesubelement in atagelement. function Zero or more EL functions (see Functions) defined in the tag library. tag-extension (optional) Extensions that provide extra information about the tag library for tools.
Top-Level Tag Library Descriptor Elements
This section describes some top-level TLD elements. Subsequent sections describe how to declare tags defined in tag files, how to declare tags defined in tag handlers, and how to declare tag attributes and variables.
validator Element
This element defines an optional tag library validator that can be used to validate the conformance of any JSP page importing this tag library to its requirements. Table 15-7 lists the subelements of the
validatorelement.
Table 15-7 validator Subelements Element Descriptionvalidator-class The class implementingjavax.servlet.jsp.tagext.TagLibraryValidatorinit-param (optional) Initialization parameters
listener Element
A tag library can specify some classes that are event listeners (see Handling Servlet Life-Cycle Events). The listeners are listed in the TLD as
listenerelements, and the web container will instantiate the listener classes and register them in a way analogous to that of listeners defined at the WAR level. Unlike WAR-level listeners, the order in which the tag library listeners are registered is undefined. The only subelement of thelistenerelement is thelistener-classelement, which must contain the fully qualified name of the listener class.Declaring Tag Files
Although not required for tag files, providing a TLD allows you to share the tag across more than one tag library and lets you import the tag library using a URI instead of the
tagdirattribute.tag-file TLD Element
A tag file is declared in the TLD using a
tag-fileelement. Its subelements are listed in Table 15-8.
Unpackaged Tag Files
Tag files placed in a subdirectory of
/WEB-INF/tags/do not require a TLD file and don't have to be packaged. Thus, to create reusable JSP code, you simply create a new tag file and place the code inside it.The web container generates an implicit tag library for each directory under and including
/WEB-INF/tags/. There are no special relationships between subdirectories; they are allowed simply for organizational purposes. For example, the following web application contains three tag libraries:/WEB-INF/tags/ /WEB-INF/tags/a.tag /WEB-INF/tags/b.tag /WEB-INF/tags/foo/ /WEB-INF/tags/foo/c.tag /WEB-INF/tags/bar/baz/ /WEB-INF/tags/bar/baz/d.tagThe implicit TLD for each library has the following values:
tlib-versionfor the tag library. Defaults to 1.0.short-nameis derived from the directory name. If the directory is/WEB-INF/tags/, the short name is simplytags. Otherwise, the full directory path (relative to the web application) is taken, minus the/WEB-INF/tags/prefix. Then all/characters are replaced with -(hyphen), which yields the short name. Note that short names are not guaranteed to be unique.- A
tag-fileelement is considered to exist for each tag file, with the following subelements:So, for the example, the implicit TLD for the
/WEB-INF/tags/bar/baz/directory would be as follows:<taglib> <tlib-version>1.0</tlib-version> <short-name>bar-baz</short-name> <tag-file> <name>d</name> <path>/WEB-INF/tags/bar/baz/d.tag</path> </tag-file> </taglib>Despite the existence of an implicit tag library, a TLD in the web application can still create additional tags from the same tag files. To accomplish this, you add a
tag-fileelement with apaththat points to the tag file.Packaged Tag Files
Tag files can be packaged in the
/META-INF/tags/directory in a JAR file installed in the/WEB-INF/lib/directory of the web application. Tags placed here are typically part of a reusable library of tags that can be used easily in any web application.Tag files bundled in a JAR require a tag library descriptor. Tag files that appear in a JAR but are not defined in a TLD are ignored by the web container.
When used in a JAR file, the
pathsubelement of thetag-fileelement specifies the full path of the tag file from the root of the JAR. Therefore, it must always begin with/META-INF/tags/.Tag files can also be compiled into Java classes and bundled as a tag library. This is useful when you wish to distribute a binary version of the tag library without the original source. If you choose this form of packaging, you must use a tool that produces portable JSP code that uses only standard APIs.
Declaring Tag Handlers
When tags are implemented with tag handlers written in Java, each tag in the library must be declared in the TLD with a
tagelement. Thetagelement contains the tag name, the class of its tag handler, information on the tag's attributes, and information on the variables created by the tag (see Tags That Define Variables).Each attribute declaration contains an indication of whether the attribute is required, whether its value can be determined by request-time expressions, the type of the attribute, and whether the attribute is a fragment. Variable information can be given directly in the TLD or through a tag extra info class. Table 15-9 lists the subelements of the
tagelement.
Table 15-9 tag Subelements Element Descriptiondescription (optional) A description of the tag.display-name (optional) name intended to be displayed by tools.icon (optional) Icon that can be used by tools.name The unique tag name.tag-class The fully qualified name of the tag handler class.tei-class (optional) Subclass ofjavax.servlet.jsp.tagext.TagExtraInfo. See Declaring Tag Variables for Tag Handlers.body-content The body content type. See body-content Element .variable (optional) Declares an EL variable exposed by the tag to the calling page. See Declaring Tag Variables for Tag Handlers.attribute Declares an attribute of the custom tag. See Declaring Tag Attributes for Tag Handlers. dynamic-attributes Whether the tag supports additional attributes with dynamic names. Defaults tofalse. If true, the tag handler class must implement thejavax.servlet.jsp.tagext.DynamicAttributesinterface. example (optional) Informal description of an example use of the tag. tag-extension (optional) Extensions that provide extra information about the tag for tools.
body-content Element
You specify the type of body that is valid for a tag by using the
body-contentelement. This element is used by the web container to validate that a tag invocation has the correct body syntax and is used by page-composition tools to assist the page author in providing a valid tag body. There are three possible values:
tagdependent: The body of the tag is interpreted by the tag implementation itself, and is most likely in a different language, for example, embedded SQL statements.empty: The body must be empty.scriptless: The body accepts only static text, EL expressions, and custom tags. No scripting elements are allowed.Declaring Tag Attributes for Tag Handlers
For each tag attribute, you must specify whether the attribute is required, whether the value can be determined by an expression, the type of the attribute in an
attributeelement (optional), and whether the attribute is a fragment. If thertexprvalueelement istrueoryes, then thetypeelement defines the return type expected from any expression specified as the value of the attribute. For static values, the type is alwaysjava.lang.String. An attribute is specified in a TLD in anattributeelement. Table 15-10 lists the subelements of theattributeelement.
If a tag attribute is not required, a tag handler should provide a default value.
The
tagelement for a tag that outputs its body if a test evaluates totruedeclares that thetestattribute is required and that its value can be set by a runtime expression.<tag> <name>present</name> <tag-class>condpkg.IfSimpleTag</tag-class> <body-content>scriptless</body-content> ... <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> ... </tag>Declaring Tag Variables for Tag Handlers
The example described in Tags That Define Variables defines an EL variable
departmentName:<tlt:iterator var="departmentName" type="java.lang.String" group="${myorg.departmentNames}"> <tr> <td><a href="list.jsp?deptName=${departmentName}"> ${departmentName}</a></td> </tr> </tlt:iterator>When the JSP page containing this tag is translated, the web container generates code to synchronize the variable with the object referenced by the variable. To generate the code, the web container requires certain information about the variable:
There are two ways to provide this information: by specifying the
variableTLD subelement or by defining a tag extra info class and including thetei-classelement in the TLD (see TagExtraInfo Class). Using thevariableelement is simpler but less dynamic. With thevariableelement, the only aspect of the variable that you can specify at runtime is its name (via thename-from-attributeelement). If you provide this information in a tag extra info class, you can also specify the type of the variable at runtime.Table 15-11 lists the subelements of the
variableelement.
Table 15-11 variable Subelements Element Descriptiondescription (optional) A description of the variable.name-given | name-from-attribute Defines an EL variable to be used in the page invoking this tag. Eithername-givenorname-from-attributemust be specified. Ifname-givenis specified, the value is the name of the variable. Ifname-from-attributeis specified, the value is the name of an attribute whose (translation-time) value at the start of the tag invocation will give the name of the variable.Translation errors arise in the following circumstances:1. Specifying neithername-givennorname-from-attributeor both.2. If twovariableelements have the samename-given.variable-class (optional) The fully qualified name of the class of the object.java.lang.Stringis the default.declare (optional) Whether or not the object is declared.Trueis the default. A translation error results if bothdeclareandfragmentare specified.scope (optional) The scope of the variable defined. Can be eitherAT_BEGIN,AT_END, orNESTED(see Table 15-12). Defaults toNESTED.
Table 15-12 summarizes a variable's availability according to its declared scope.
You can define the following
variableelement for thetlt:iteratortag: