Tag Library Descriptor

In this post, you will learn:

  • What is a Tag Library Descriptor (TLD)?
  • What elements are used to define a TLD?
  • How a TLD is referenced in a JSP?
  • How a custom tag is used in a JSP?

Tag Library Descriptor:

A Tag Library Descriptor (TLD) is a XML file crated by the tag programmer to associate custom tags to their Tag classes. With the help of a TLD file, a Tag programmer groups the logically related tags to a library. It is used by the Tag Processor to identify the Tag classes and their attributes. It has following elements:


<taglib>

<uri>unique identifier of the TLD file</uri>

<tlib-version>Version of Tag Library </tlib-version>
<jsp-version>Version of JSP API</jsp-version>

<tag>
<name>Tag Name</name>
<tag-class>Fully Qualified Tag Class</tag-class>
<attribute>
<name>Attribute Name</name>
<required>true/false</required>
<rtexprvalue>true/false</rtexprvalue>
</attribute>
...
</tag>
...
</taglib>

Description of the Tag Library Descriptor elements:

1. <taglib> is the root element of the TLD file i.e. a TLD file starts and ends with it.

2. <uri> is an optional element. It is used to assign a unique identifier to the TLD file. This identifier is used in the JSP pages to identify the TLD file. When its value is not specified in the TLD file, the file is referenced in the JSP using its path.

3. <tlib-version> is a required element. It is used to specify the version of the tag library.

4. <jsp-version> is a required element. It is used to specify the version of the JSP API required for the execution of the tags.

5. <tag> is a required element. It is used to define the mapping of a tag. For each tag of the library a <tag> element is required in the TLD file.

6. <name> is a required sub element of <tag>. It is used to specify the tag name for which the mapping is being defined.

7. <tag-class> is a required sub element of <tag>. It is used to specify the Tag class for the tag.

8. <attribute> is an optional sub element of <tag>. It is used to specify the configuration of tag attributes. For each attribute of the tag, an <attribute> element is required.

9. <name> is a required sub element of <attribute>. It is used to specify the name of the attribute. Attribute name is used by the Tag Processor to identify its setter method in the Tag class.

10. <required> is an optional sub element of <attribute>. It is used to specify whether the attribute is required or not. Its default value is false i.e. by default a tag attribute is optional.

11. <rtexprvalue> is an optional sub element of <attribute>. It is used to specify whether expression tag can be used to set the value of the attribute or not. Its default value is false i.e. by default expression tag can’t be used to set the value of an attribute.

Notes:

1. A TLD file is to be saved under the WEB-INF folder if the tag library is directly used in a project.or under META-INF folder of the JAR if the tag library is packed in a JAR file.

2. If the tag library is packed in a JAR file then the TLD file is saved in META-INF folder of the JAR.

3. In addition to the above, a TLD file may have other optional elements as well.

Referencing a Tag Library Descriptor in a JSP:

taglib directive of the JSP is used to reference a TLD file in JSP page. It assigns a library name to the tags defined in a TLD file so that the tags of the file can be referenced as part of the library. It has following syntax:

<%@ taglib uri=”unique identifier or path of the TLD file” prefix=”library name” %>

Syntax of using a custom tag in a JSP:

Tag without body:

<libraryName:tagName attribute=”value” … />

Tag with body:

<libraryName:tagName attribute=”value” … >

Tag Body

</libraryName:tagName >

Attributes are used to pass information from the JSP page to the Tag object. Tag Body represents contents which are to be formatted or presented by the Tag to the end user.


Its time to recap the main concepts:

  1. A TLD file is used by the tag programmer to group logically related tags in a single library.
  2. It contains the mapping of tags and their classes. This mapping is used by the Tag processor to identify the Tag classes of the tags which are used in the JSP pages.
  3. taglib directive is used in JSP pages to refer the tags of a TLD file using a logical library name.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *