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.
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:
Syntax of using a custom tag in a JSP:
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.