Tag Libraries and Tag Library Descriptor (TLD)

In this short article, we will look into what is Tag library is and the usage of TLD files in the JSP applications.

JSP Tag Libraries

Custom tags are implemented and distributed in a structure known as a tag library, sometimes referred to as a taglib
A tag library is a collection of classes and meta-information that includes
  • Tag Handlers are Java classes that implement the functionality of custom tags.
  • Tag Extra Information Classes that supply the JSP container with logic for validating tag attributes and creating scripting variables.
  • A Tag Library Descriptor (TLD) is an XML document that describes the properties of the individual tags and the tag library as a whole.
The tag handler and tag extra information classes need to be located where they can be found by the JSP container classloader. The tld files should be present under the WEB-INF directory.

The Tag Library Descriptor (TLD)

The tag library configuration information needed by a JSP container is stored in a Tag Library Descriptor (TLD)
A TLD is an XML document that describes the individual tags in the library, their tag handlers, and attributes, as well as version and identifying information about the library as a whole.

TLD Elements

TLD consists of a single element having certain subelements in a fixed order:
  • tlibversion - This is a required element containing the version number of the tag library. This is a dotted-decimal number consisting of up to four groups of digits separated by decimal points, such as “1.0”, or “1.3.045”.
  • jspversion - This is an optional element identifying the minimal level of the JSP specification required to support the tag library. For example, for JSP version 2.0, this would be “2.0”.
  • shortname - This is a short descriptive name that identifies the tag library.
  • uri - This is an optional element that defines a unique URI, which identifies this library. This is typically the URL of the location from which the latest version of the taglib can be downloaded.
  • info This is an optional element in which descriptive information about the tag library is entered. This is intended for human viewing in a JSP authoring tool.
  • tag - One or more tag entries can be in a TLD. These describe the individual tags that comprise the library.
A tag element itself consists of up to six types of subelements:
  • name - The tag name will be used on a JSP page. Together with a namespace prefix that identifies the tag library, the name uniquely identifies a tag to the JSP container.
  • tagclass - A required element consisting of the fully qualified name of the tag handler that implements the tag.
  • teiclass - An optional element consisting of the fully qualified name of the Tag Extra Information (TEI) class used by this tag, if any.
  • bodycontent - Optionally describes how the tag handler uses its body content. The possible values are
  • empty - The tag body must be empty
  • JSP The tag body consists of other JSP elements
  • tagdependent - The tag body is interpreted by the tag itself, with no JSP transformations
  • info - Optional human-readable descriptive information about the tag.
  • attribute - Optional information about attributes that can be coded when the tag is used in a JSP page.
Let's demonstrate the usage of a few important TLD elements with an example.

Tag Library Descriptor (TLD) Example

<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>My Custom Tag</short-name>
    <tag>
        <name>CustomTag</name>
        <tag-class>net.javaguides.jsp.tutorial.jstl.CustomTag</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>

The taglib Directive

The purpose of the taglib directive is to specify the location of the TLD and assign it a short alias (prefix) that distinguishes its tags on this page.

Syntax JSP Taglib directive

<%@ taglib prefix="tag prefix" uri="taglibURI" %>
Where the two attributes are
  • tag prefix - A name, unique on this page, used to identify tags from this library.
  • taglibURI - The URI of the tag library itself. This can be an absolute pathname beginning with / that is interpreted relative to the top of the Web application. Or, it can be a URL that acts as a symbolic name for the TLD. In this case, the name must be mapped to the actual TLD by means of an entry in the Web.xml file.
Read a complete example of custom tag development at JSP Custom Tag Development.

Comments