Creating a Tag Library Descriptor
The following sections describe how to create a tag library Bdescriptor (TLD) file:
- Overview of Tag Library Descriptors
- Writing the Tag Library Descriptor
- Sample Tag Library Descriptor
Overview of Tag Library Descriptors
A tag library allows a developer to group together tags with related functionality. A tag library uses a tag library descriptor (tld) file that describes the tag extensions and relates them to their Java classes. WebLogic Server and some authoring tools use the TLD to get information about the extensions. TLD files are written in XML notation.
The syntax for a tag library descriptor is specified in the document type descriptor (DTD) available at: http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd.
Writing the Tag Library Descriptor
Order the elements in the tag library descriptor file as they are defined in the DTD. This ordering is used in the following procedure. The XML parser throws an exception if you incorrectly order the TLD elements.
The body of the TLD contains additional nested elements inside of the <taglib> ... </taglib> element. These nested elements are also described in the steps below. For display in this document, nested elements are indented from their parent elements, but indenting is not required in the TLD.
The example in Sample Tag Library Descriptor declares a new tag called code. The functionality of this tag is implemented by the Java class weblogic.taglib.quote.CodeTag.
To create a tag library descriptor:
- Create a text file with an appropriate name and the extension .tld, and locate it in the WEB-INF directory of the Web Application containing your JSP(s). Content beneath the WEB-INF directory is non-public and is not served over HTTP by WebLogic Server.
- Include the following header:
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd";>- Add the contents of the TLD, embedded in a <taglib> element. The contents include elements containing information about the tag library and elements that define each tag. For example:
<taglib> ... body of taglib descriptor ...
</taglib>- Identify the tag library:
(Required) Describes the JSP specification version (number) this tag library requires in order to function. The default is 1.2.
(Required) Assigns a short name to this tag library. This element is not used by WebLogic Server.
(Required) Defines a public URI that uniquely identifies this version of the tag library.
(Optional) Contains a short name that is intended to be displayed by tools.
(Optional) Contains the name of a file containing a small (16 x 16) icon image. The file name is relative path within the tag library. The image must be either in the JPEG or GIF format, and the file name must end with the suffix ".jpg" or ".gif" respectively. The icon can be used by tools.
(Optional) Contains the name of a file containing a large (32 x 32) icon image. The file name is relative path within the tag library. The image must be either in the JPEG or GIF format, and the file name must end with the suffix ".jpg" or ".gif" respectively. The icon can be used by tools.
(Required) Defines an arbitrary text string describing the tag library.
(Optional) Provides information on the scripting variables defined by this tag. It is a (translation-time) error for a tag that has one or more variable subelements to have a TagExtraInfo class that returns a non-null object.
(Optional) Defines an optional event listener object to be instantiated and registered automatically.
- Define a tag library validator (Optional).
(Optional) Defines initialization parameters for the validator class.
- Define a tag.
Use a separate <tag> element to define each new tag in the tag library. The <tag> element takes the following nested tags:
(Required) Defines the name of the tag. This is used when referencing the tag in a JSP file, after the ":" symbol, For example:
For more information, see Using Custom Tags in a JSP.
(Required) Declares the tag handler class that implements the functionality of this tag. Specify the fully qualified package name of the class.
Locate the class file under the WEB-INF/classes directory, in a directory structure reflecting the package name. You can also package the classes in a tag library jar file; for more information, see Deploying a JSP Tag Library as a JAR File.
(Optional) Declares the subclass of TagExtraInfo that describes the scripting variables introduced by this tag. If your tag does not define new scripting variables, it does not use this element. Specify the fully qualified package name of the class. You can perform validation of the tag's attributes in this class.
Place the class files under the WEB-INF/classes directory of your Web Application, under a directory structure reflecting the package name. You can also package the classes in a tag library jar file; for more information, see Deploying a JSP Tag Library as a JAR File.
empty means that you use the tag in the empty tag format in the JSP page. For example: <taglib:tagname/>
JSP means that the contents of the tag can be interpreted as JSP and that use the tag in the body tag format. For example:
tagdependent means that your tag will interpret the contents of the body as non-JSP (for instance an SQL statement).
If the <body-content> element is not defined, the default value is JSP.
(Optional) Defines the name of the attribute as it appears in the tag element in the JSP page. For example:
Use a separate <attribute> element to define each attribute that the tag can take. Tag attributes allow the JSP author to alter the behavior of your tags.
(Optional) Defines whether this attribute has optional use in the JSP page.
If not defined here, the default is false - that is, the attribute is optional by default.
If true is specified, and the attribute is not used in a JSP page, a translation-time error occurs.
(Optional) Defines whether this attribute can take a scriptlet expression as a value, allowing it to be dynamically calculated at request time.
If this element is not specified, the value is presumed to be false.
- Define scripting variables (optional).
Within the <tag> element, you can define scripting variables.
Defines the name of the variable, or you can define the name from an attribute using
(Optionals) If set to true, indicates that the variable is to be defined.
AT_BEGIN (The variable is defined just before executing the body)
AT_END (The variable is defined just after executing the body.)
Sample Tag Library Descriptor
The following is a sample listing of a tag library descriptor.
Listing 3-1 Sample Tag Library Descriptor (tld)
<taglib><tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>quote</short-name>
<uri>tag lib version id</uri>
<description> This tag library contains several tag extensions useful for formatting content for HTML. </description><tag> <name>code</name> <tag-class>weblogic.taglib.quote.CodeTag</tag-class> <body-content>tagdependent</body-content> <attribute> <name>fontAttributes</name> </attribute> <attribute> <name>commentColor</name> </attribute> <attribute> <name>quoteColor</name> </attribute> </tag>
</taglib>