|Oracle® Fusion Middleware Programming JSP Tag Extensions for Oracle WebLogic Server
12c Release 1 (12.1.1)
Part Number E24388-01
|PDF · Mobi · ePub|
This chapter describes how to create a tag library descriptor (TLD).
This chapter includes the following sections:
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 have the file extension .tld and are written in XML notation.
Order the elements in the tag library descriptor file as they are defined in the XSD. 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
To create a TLD:
Create a text file with an appropriate name and the extension .tld, and save 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:
<taglib version="2.0" xmlns="http://java.sun.com/xml/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
Add the contents of the TLD, embedded in a
<taglib> element, as indicated in steps 4-7. 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) The version number of the tag library.
(Required) Describes the JSP specification version (number) this tag library requires in order to function. The default is 2.0.
(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 a 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 a 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. A translation-time error occurs if a tag that has one or more variable subelements has 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).
Top level element for a validator.
(Required) The Java class that performs the validation.
(Optional) Defines initialization parameters for the validator class.
Defines the name of this parameter.
Defines the name of this parameter.
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 New and Changed JSP Features In This Release.
(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 Packaging a JSP Tag Library as 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 Packaging a JSP Tag Library as JAR File.
<body-content>empty | JSP | scriptless | tagdependent</body-content>
(Optional) Defines the content of the tag body.
empty means that you use the tag in the empty tag format in the JSP page. For example:
JSP means that the contents of the tag can be interpreted as a JSP and that you must use the tag in the body tag format. For example:
scriptless means that the contents of the tag do not contain any scripts or scripting elements.
tagdependent means that your tag will interpret the contents of the body as a non-JSP (for example, an SQL statement).
(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.
<name>myAttribute</name> <required>true | false</required>
(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.
<rtexprvalue>true | false</rtexprvalue>
(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
Define scripting variables (optional).
<tag> element, you can define scripting variables.
Top level element for declaring a variable.
Defines the name of the variable, or you can define the name from an attribute using:
Names the variable with the value of
The Java type of this variable.
(Optional) If set to true, indicates that the variable is to be defined.
The scope of the scripting variable. Valid options are:
NESTED (The variable is only available inside the tag body)
AT_BEGIN (The variable is defined just before executing the body)
AT_END (The variable is defined just after executing the body.)
The following is a sample listing of a tag library descriptor.
Example 3-1 Sample Tag Library Descriptor (tld)
<taglib version="2.0" xmlns="http://java.sun.com/xml/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"> <taglib> <tlib-version>2.0</tlib-version> <jsp-version>2.0</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>
The DynamicAttributes interface, supported by the JSP 2.0 container, allows you to use tags with values that are treated in a consistent manner but with names that are not necessarily known at development time. For example, if you want to customize the
width portion of the
<table> HTML tag by determining its value at runtime, you could make use of the DynamicAttributes interface to customize only the portion of the tag you want to change, without needing to specify anything about the other portions of the
<table> tag (
cellspacing, and so on). Without the DynamicAttributes interface, you would have needed to write a Tag Handler or other complicated code to accomplish the task of determining the value of
width dynamically at runtime.
The TLD is what ultimately determines whether a tag handler accepts dynamic attributes or not. If a tag handler declares that it supports dynamic attributes in the TLD but it does not implement the
DynamicAttributes interface, the tag handler must be considered invalid by the container.
dynamic-attributes element (a child element to the
tag element for the tag library being authored) for a tag being invoked contains the value true, the following requirements apply:
For each attribute specified in the tag invocation that does not have a corresponding attribute element in the TLD for this tag, a call must be made to
setDynamicAttribute(), passing in the namespace of the attribute (or null if the attribute does not have a namespace or prefix), the name of the attribute without the namespace prefix, and the final value of the attribute.
Dynamic attributes must be considered to accept request-time expression values.
Dynamic attributes must be treated as though they were of type
The JSP container must recognize dynamic attributes that are passed to the tag handler using the <jsp:attribute> standard action.
setDynamicAttribute() method throws
doTag() method is not invoked for this tag, and the exception must be treated in the same manner as if it came from a regular attribute setter method.
For a JSP document in either standard or XML syntax, if a dynamic attribute has a prefix that does not map to a namespace, a translation error must occur. In standard syntax, only namespaces defined using taglib directives are recognized.
In the following example, assume attributes
b are declared using the attribute element in the TLD, attributes
d2 are not declared, and the
dynamic-attributes element is set to true. You set the attributes using the calls:
setA( "1" ),
setDynamicAttribute( null, "d1", "2" ),
setDynamicAttribute( "http://www.foo.com/jsp/taglib/mytag.tld", "d2", "3" ),
setB( "4" ),
setDynamicAttribute( null, "d3", "5" ), and
setDynamicAttribute( "http://www.foo.com/jsp/taglib/mytag.tld", "d4", "6" ).
Example 3-2 Dynamic Attribute Example
<jsp:root xmlns:mytag="http://www.foo.com/jsp/taglib/mytag.tld" version="2.0"> <mytag:invokeDynamic a="1" d1="2" mytag:d2="3"> <jsp:attribute name="b">4</jsp:attribute> <jsp:attribute name="d3">5</jsp:attribute> <jsp:attribute name="mytag:d4">6</jsp:attribute> </mytag:invokeDynamic> </jsp:root>
For a tag to declare that it accepts dynamic attributes, it must implement the
DynamicAttributes interface. The syntax is as follows:
public interface DynamicAttributes
You must also configure an entry for the tag in the TLD to indicate dynamic attributes are accepted. For any attribute that is not declared in the TLD for this tag, instead of getting an error at translation time, the
setDynamicAttribute() method is called, with the name and value of the attribute. You configure the tag to remember the names and values of the dynamic attributes.
setDynamicAttribute() method is called when a tag declared to accept dynamic attributes passes an attribute that is not declared in the TLD. The syntax is as follows:
public void setDynamicAttribute(java.lang.String uri, java.lang.String localName, java.lang.Object value)
The parameter values are as follows:
uri - the namespace of the attribute, or null if in the default namespace.
localName - the name of the attribute being set.
value - the value of the attribute.
JspException is thrown if the tag handler signals that it does not accept the given attribute. The container must not call
doTag() for this tag.
For more information on these interfaces, refer to the DynamicAttributes API at