|
Home TOC Index |
|
Search
Feedback |
Defining Tags
This section describes the properties of tag handlers and TLDs and explains how to develop tag handlers and library descriptor elements for each type of tag introduced in the previous section.
Tag Handlers
A tag handler is an object invoked by a Web container to evaluate a custom tag during the execution of the JSP page that references the tag. Tag handlers must implement either the
TagorBodyTaginterface. Interfaces can be used to take an existing Java object and make it a tag handler. For newly created handlers, you can use theTagSupportandBodyTagSupportclasses as base classes. These classes and interfaces are contained in thejavax.servlet.jsp.tagextpackage.Tag handler methods defined by the
TagandBodyTaginterfaces are called by the JSP page's servlet at various points during the evaluation of the tag. When the start tag of a custom tag is encountered, the JSP page's servlet calls methods to initialize the appropriate handler and then invokes the handler'sdoStartTagmethod. When the end tag of a custom tag is encountered, the handler'sdoEndTagmethod is invoked. Additional methods are invoked in between when a tag handler needs to interact with the body of the tag. For further information, see How Is a Tag Handler Invoked?. In order to provide a tag handler implementation, you must implement the methods, summarized in Table 13-1, that are invoked at various stages of processing the tag.A tag handler has access to an API that allows it to communicate with the JSP page. The entry point to the API is the page context object (
javax.servlet.jsp.PageContext), through which a tag handler can retrieve all the other implicit objects (request, session, and application) accessible from a JSP page.Implicit objects can have named attributes associated with them. Such attributes are accessed using
[set|get]Attributemethods.If the tag is nested, a tag handler also has access to the handler (called the parent) associated with the enclosing tag.
A set of related tag handler classes (a tag library) is usually packaged and deployed as a JAR archive.
Tag Library Descriptors
A tag library descriptor (TLD) is an XML document that describes a tag library. A TLD contains information about a library as a whole and about each tag contained in the library. TLDs are used by a Web container to validate the tags and by JSP page development tools.
TLD file names must have the extension
.tld. TLD files are stored in theWEB-INFdirectory of the WAR file or in a subdirectory ofWEB-INF. When you add a TLD to a WAR usingdeploytool, it automatically puts it intoWEB-INF.A TLD must begin with an XML document prolog that specifies the version of XML and the document type definition (DTD):
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">The J2EE SDK version 1.3 can understand version 1.1 and 1.2 DTDs. However, this chapter documents the 1.2 version because you should use the newer version in any tag libraries that you develop. The template library TLD,
tutorial-template.tld, conforms to the 1.2 version. The Struts library TLDs conform to the 1.1 version of the DTD, which has fewer elements and uses slightly different names for some of the elements.The root of a TLD is the
taglibelement. The subelements oftaglibare listed in Table 13-2:
Table 13-2 taglib Subelements Element
Description
tlib-versionThe tag library's version
jsp-versionThe JSP specification version that the tag library requires
short-nameOptional name that could be used by a JSP page authoring tool to create names with a mnemonic value
uriA URI that uniquely identifies the tag library
display-nameOptional name intended to be displayed by tools
small-iconOptional small icon that can be used by tools
large-iconOptional large icon that can be used by tools
descriptionOptional tag-specific information
listenerSee listener Element
tagSee tag Element
listener Element
A tag library can specify some classes that are event listeners (see Handling Servlet Life-Cycle Events). The listeners are listed in the TLD as
listenerelements, and the Web container will instantiate the listener classes and register them in a way analogous to listeners defined at the WAR level. Unlike WAR-level listeners, the order in which the tag library listeners are registered is undefined. The only subelement of thelistenerelement is thelistener-classelement, which must contain the fully qualified name of the listener class.tag Element
Each tag in the library is described by giving its name and the class of its tag handler, information on the scripting variables created by the tag, and information on the tag's attributes. Scripting variable information can be given directly in the TLD or through a tag extra info class (see Tags That Define Scripting Variables). Each attribute declaration contains an indication of whether the attribute is required, whether its value can be determined by request-time expressions, and the type of the attribute (see Tags with Attributes).
A tag is specified in a TLD in a
tagelement. The subelements oftagare listed in Table 13-3:
Table 13-3 tag Subelements Element
Description
nameThe unique tag name
tag-classThe fully-qualified name of the tag handler class
tei-classOptional subclass of javax.servlet.jsp.tagext.TagExtraInfo. See TagExtraInfo Class
body-contentThe body content type. See body-content Element and body-content Element
display-nameOptional name intended to be displayed by tools.
small-iconOptional small-icon that can be used by tools.
large-iconOptional large-icon that can be used by tools.
descriptionOptional tag-specific information.
variableOptional scripting variable information. See variable Element.
attributeTag attribute information. See attribute Element.
The following sections describe the methods and TLD elements that you need to develop for each type of tag introduced in Using Tags.
Simple Tags
Tag Handlers
The handler for a simple tag must implement the
doStartTaganddoEndTagmethods of theTaginterface. ThedoStartTagmethod is invoked when the start tag is encountered. This method returnsSKIP_BODYbecause a simple tag has no body. ThedoEndTagmethod is invoked when the end tag is encountered. ThedoEndTagmethod needs to returnEVAL_PAGEif the rest of the page needs to be evaluated; otherwise, it should returnSKIP_PAGE.The simple tag discussed in the first section,
<tt:simple />would be implemented by the following tag handler:
public SimpleTag extends TagSupport { public int doStartTag() throws JspException { try { pageContext.getOut().print("Hello."); } catch (Exception ex) { throw new JspTagException("SimpleTag: " + ex.getMessage()); } return SKIP_BODY; } public int doEndTag() { return EVAL_PAGE; } }body-content Element
Tags without bodies must declare that their body content is empty using the
body-contentelement:<body-content>empty</body-content>Tags with Attributes
Defining Attributes in a Tag Handler
For each tag attribute, you must define a property and
getandsetmethods that conform to the JavaBeans architecture conventions in the tag handler. For example, the tag handler for the Strutslogic:presenttag,<logic:present parameter="Clear">contains the following declaration and methods:
protected String parameter = null; public String getParameter() { return (this.parameter); } public void setParameter(String parameter) { this.parameter = parameter; }Note that if your attribute is named
idand your tag handler inherits from theTagSupportclass, you do not need to define the property and set and get methods because these are already defined byTagSupport.A tag attribute whose value is a
Stringcan name an attribute of one of the implicit objects available to tag handlers. An implicit object attribute would be accessed by passing the tag attribute value to the [set|get]Attributemethod of the implicit object. This is a good way to pass scripting variable names to a tag handler where they are associated with objects stored in the page context (see Tags That Define Scripting Variables).attribute Element
For each tag attribute, you must specify whether the attribute is required, whether the value can be determined by an expression, and, optionally, the type of the attribute in an
attributeelement. For static values the type is alwaysjava.lang.String. If thertexprvalueelement istrueoryes, then thetypeelement defines the return type expected from any expression specified as the value of the attribute.<attribute> <name>attr1</name> <required>true|false|yes|no</required> <rtexprvalue>true|false|yes|no</rtexprvalue> <type>fully_qualified_type</type> </attribute>If a tag attribute is not required, a tag handler should provide a default value.
The
tagelement for thelogic:presenttag declares that theparameterattribute is not required (because the tag can also test for the presence of other entities such as bean properties) and that its value can be set by a runtime expression.<tag> <name>present</name> <tag-class>org.apache.struts.taglib. logic.PresentTag</tag-class> <body-content>JSP</body-content> ... <attribute> <name>parameter</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> ... </tag>Attribute Validation
The documentation for a tag library should describe valid values for tag attributes. When a JSP page is translated, a Web container will enforce any constraints contained in the TLD element for each attribute.
The attributes passed to a tag can also be validated at translation time with the
isValidmethod of a class derived fromTagExtraInfo. This class is also used to provide information about scripting variables defined by the tag (see Tags That Define Scripting Variables).The
isValidmethod is passed the attribute information in aTagDataobject, which contains attribute-value tuples for each of the tag's attributes. Since the validation occurs at translation time, the value of an attribute that is computed at request time will be set toTagData.REQUEST_TIME_VALUE.The tag
<tt:twaattr1="value1"/>has the following TLDattributeelement:<attribute> <name>attr1</name> <required>true</required> <rtexprvalue>true</a> </attributeThis declaration indicates that the value of
attr1can be determined at runtime.The following
isValidmethod checks that the value ofattr1is a valid Boolean value. Note that since the value ofattr1can be computed at runtime,isValidmust check whether the tag user has chosen to provide a runtime value.public class TwaTEI extends TagExtraInfo { public boolean isValid(Tagdata data) { Object o = data.getAttribute("attr1"); if (o != null && o != TagData.REQUEST_TIME_VALUE) { if (o.toLowerCase().equals("true") || o.toLowerCase().equals("false") ) return true; else return false; } else return true; } }Tags With Bodies
Tag Handlers
A tag handler for a tag with a body is implemented differently depending on whether the tag handler needs to interact with the body or not. By interact, we mean that the tag handler reads or modifies the contents of the body.
Tag Handler Does Not Interact with the Body
If the tag handler does not need to interact with the body, the tag handler should implement the
Taginterface (or be derived fromTagSupport). If the body of the tag needs to be evaluated, thedoStartTagmethod needs to returnEVAL_BODY_INCLUDE; otherwise, it should returnSKIP_BODY.If a tag handler needs to iteratively evaluate the body, it should implement the
IterationTaginterface or be derived fromTagSupport. It should returnEVAL_BODY_AGAINfrom thedoStartTaganddoAfterBodymethods if it determines that the body needs to be evaluated again.Tag Handler Interacts with the Body
If the tag handler needs to interact with the body, the tag handler must implement
BodyTag(or be derived fromBodyTagSupport). Such handlers typically implement thedoInitBodyand thedoAfterBodymethods. These methods interact with body content passed to the tag handler by the JSP page's servlet.Body content supports several methods to read and write its contents. A tag handler can use the body content's
getStringorgetReadermethods to extract information from the body, and thewriteOut(out)method to write the body contents to an out stream. The writer supplied to thewriteOutmethod is obtained using the tag handler'sgetPreviousOutmethod. This method is used to ensure that a tag handler's results are available to an enclosing tag handler.If the body of the tag needs to be evaluated, the
doStartTagmethod needs to returnEVAL_BODY_BUFFERED; otherwise, it should returnSKIP_BODY.The
doInitBodymethod is called after the body content is set but before it is evaluated. You generally use this method to perform any initialization that depends on the body content.The
doAfterBodymethod is called after the body content is evaluated. Like thedoStartTagmethod,doAfterBodymust return an indication of whether to continue evaluating the body. Thus, if the body should be evaluated again, as would be the case if you were implementing an iteration tag,doAfterBodyshould returnEVAL_BODY_BUFFERED; otherwisedoAfterBodyshould returnSKIP_BODY.A tag handler should reset its state and release any private resources in the
releasemethod.The following example reads the content of the body (which contains a SQL query) and passes it to an object that executes the query. Since the body does not need to be reevaluated,
doAfterBodyreturnsSKIP_BODY.public class QueryTag extends BodyTagSupport { public int doAfterBody() throws JspTagException { BodyContent bc = getBodyContent(); // get the bc as string String query = bc.getString(); // clean up bc.clearBody(); try { Statement stmt = connection.createStatement(); result = stmt.executeQuery(query); } catch (SQLException e) { throw new JspTagException("QueryTag: " + e.getMessage()); } return SKIP_BODY; } }body-content Element
For tags that have a body, you must specify the type of the body content using the
body-contentelement:<body-content>JSP|tagdependent</body-content>Body content containing custom and core tags, scripting elements, and HTML text is categorized as
JSP. This is the value declared for the Struts logic:presenttag. All other types of body content--for example, SQL statements passed to the query tag--would be labeledtagdependent.Note that the value of the
body-contentelement does not affect the interpretation of the body by the tag handler; the element is only intended to be used by an authoring tool for rendering the body content.Tags That Define Scripting Variables
Tag Handlers
A tag handler is responsible for creating and setting the object referred to by the scripting variable into a context accessible from the page. It does this by using the
pageContext.setAttribute(name,value,scope)orpageContext.setAttribute(name,value)methods. Typically, an attribute passed to the custom tag specifies the name of the scripting variable object; this name can be retrieved by invoking the attribute'sgetmethod described in Defining Attributes in a Tag Handler.If the value of the scripting variable is dependent on an object present in the tag handler's context, it can retrieve the object using the
pageContext.getAttribute(name, scope)method.The usual procedure is that the tag handler retrieves a scripting variable, performs some processing on the object, and then sets the scripting variable's value using the
pageContext.setAttribute(name,object)method.The scope that an object can have is summarized in Table 13-4. The scope constrains the accessibility and lifetime of the object.
Providing Information about the Scripting Variable
The example described in Tags That Define Scripting Variables defines a scripting variable
bookthat is used for accessing book information:<bean:define id="book" name="bookDB" property="bookDetails" type="database.BookDetails"/> <font color="red" size="+2"> <%=messages.getString("CartRemoved")%> <strong><jsp:getProperty name="book" property="title"/></strong> <br> <br> </font>When the JSP page containing this tag is translated, the Web container generates code to synchronize the scripting variable with the object referenced by the variable. To generate the code, the Web container requires certain information about the scripting variable:
- Variable name
- Variable class
- Whether the variable refers to a new or existing object
- The availability of the variable
There are two ways to provide this information: by specifying the
variableTLD subelement or by defining a tag extra info class and including thetei-classelement in the TLD. Using thevariableelement is simpler, but slightly less flexible.variable Element
The
variableelement has the following subelements:
name-given: The variable name as a constantname-from-attribute: The name of an attribute whose translation-time value will give the name of the variableOne of
name-givenorname-from-attributeis required. The following subelements are optional:
variable-class: The fully qualified name of the class of the variable.java.lang.Stringis the default.declare: Whether the variable refers to a new object.Trueis the default.scope: The scope of the scripting variable defined.NESTEDis default. Table 13-5 describes the availability of the scripting variable and the methods in which the value of the variable must be set or reset.The implementation of the Struts
bean:definetag conforms to the JSP specification version 1.1, which requires you to define a tag extra info class. The JSP specification version 1.2 adds thevariableelement. You could define the followingvariableelement for thebean:definetag:<tag> <variable> <name-from-attribute>id</name-from-attribute> <variable-class>database.BookDetails</variable-class> <declare>true</declare> <scope>AT_BEGIN</scope> </variable> </tag>TagExtraInfo Class
You define a tag extra info class by extending the class
javax.servlet.jsp.TagExtraInfo. ATagExtraInfomust implement thegetVariableInfomethod to return an array ofVariableInfoobjects containing the following information:
- Variable name
- Variable class
- Whether the variable refers to a new object
- The availability of the variable
The Web container passes a parameter called
datato thegetVariableInfomethod that contains attribute-value tuples for each of the tag's attributes. These attributes can be used to provide theVariableInfoobject with a scripting variable's name and class.The Struts tag library provides information about the scripting variable created by the
bean:definetag in the DefineTei tag extra info class. Since the name (book) and class (database.BookDetails) of the scripting variable are passed in as tag attributes, they can be retrieved with thedata.getAttributeStringmethod and used to fill in theVariableInfoconstructor. To allow the scripting variablebookto be used in the rest of the page, the scope ofbookis set to beAT_BEGIN.public class DefineTei extends TagExtraInfo { public VariableInfo[] getVariableInfo(TagData data) { String type = data.getAttributeString("type"); if (type == null) type = "java.lang.Object"; return new VariableInfo[] { new VariableInfo(data.getAttributeString("id"), type, true, VariableInfo.AT_BEGIN) }; } }The fully qualified name of the tag extra info class defined for a scripting variable must be declared in the TLD in the
tei-classsubelement of thetagelement. Thus, thetei-classelement forDefineTeiwould be as follows:<tei-class>org.apache.struts.taglib.bean.DefineTagTei </tei-class>Cooperating Tags
Tags cooperate by sharing objects. JSP technology supports two styles of object sharing. The first style requires that a shared object be named and stored in the page context (one of the implicit objects accessible to both JSP pages and tag handlers). To access objects created and named by another tag, a tag handler uses the
pageContext.getAttribute(name,scope)method.In the second style of object sharing, an object created by the enclosing tag handler of a group of nested tags is available to all inner tag handlers. This form of object sharing has the advantage that it uses a private namespace for the objects, thus reducing the potential for naming conflicts.
To access an object created by an enclosing tag, a tag handler must first obtain its enclosing tag with the static method
TagSupport.findAncestorWithClass(from,class)or theTagSupport.getParentmethod. The former method should be used when a specific nesting of tag handlers cannot be guaranteed. Once the ancestor has been retrieved, a tag handler can access any statically or dynamically created objects. Statically created objects are members of the parent. Private objects can also be created dynamically. Such objects can be stored in a tag handler with thesetValuemethod and retrieved with thegetValue method.The following example illustrates a tag handler that supports both the named and private object approaches to sharing objects. In the example, the handler for a query tag checks whether an attribute named
connectionhas been set in thedoStartTagmethod. If theconnectionattribute has been set, the handler retrieves the connection object from the page context. Otherwise, the tag handler first retrieves the tag handler for the enclosing tag and then retrieves the connection object from that handler.public class QueryTag extends BodyTagSupport { private String connectionId; public int doStartTag() throws JspException { String cid = getConnection(); if (cid != null) { // there is a connection id, use it connection =(Connection)pageContext. getAttribute(cid); } else { ConnectionTag ancestorTag = (ConnectionTag)findAncestorWithClass(this, ConnectionTag.class); if (ancestorTag == null) { throw new JspTagException("A query without a connection attribute must be nested within a connection tag."); } connection = ancestorTag.getConnection(); } } }The query tag implemented by this tag handler could be used in either of the following ways:
<tt:connection id="con01" ....> ... </tt:connection> <tt:query id="balances" connection="con01"> SELECT account, balance FROM acct_table where customer_number = <%= request.getCustno()%> </tt:query> <tt:connection ...> <x:query id="balances"> SELECT account, balance FROM acct_table where customer_number = <%= request.getCustno()%> </x:query> </tt:connection>The TLD for the tag handler must indicate that the
connectionattribute is optional with the following declaration:<tag> ... <attribute> <name>connection</name> <required>false</required> </attribute> </tag>
|
Home TOC Index |
|
Search
Feedback |