Programming WebLogic JSP Tag Extensions
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
The following sections describe how to create a tag library descriptor (TLD):
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 weblogic.taglib.quote.CodeTag
.
.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.<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>
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>
(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 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 "WebLogic Server Known and Resolved Issues" lists known problems for WebLogic Server..
(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>
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 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).
The following is a sample listing of a tag library descriptor.
Listing 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 (border
, 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.
If the 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:
In the following example, assume attributes a and b are declared using the attribute element in the TLD, attributes d1 and d2 are not declared, and the dynamic-attributes element is set to true. You set the attributes using the calls:
Listing 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.
The 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:
A JspException is thrown if the tag handler signals that it does not accept the given attribute. The container must not call doStartTag() or doTag() for this tag.
For more information on these interfaces, refer to the DynamicAttributes API at http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/jsp/tagext/DynamicAttributes.html.
![]() ![]() |
![]() |
![]() |