Contents | Prev | Next



Tags With Attributes


Defining Attributes in a Tag Handler

For each tag attribute, you must define a property and JavaBeans style get and set methods in the tag handler. For example, the tag handler for the tag

<tlt:twa attr1="value1">
where value1 is of type AttributeClass, must contain the following declaration and methods:

private AttributeClass attr1;
setAttr1(AttributeClass ac) { ... }
AttributeClass getAttr1() { ... }
Note that if your attribute is named id, and your tag handler inherits from the TagSupport class, you do not need to define the property and set and get methods because these are already defined by TagSupport.

A tag attribute whose value is a String can 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]Attribute method 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").


TLD attribute Element

For each tag attribute you must specify whether the attribute is required, and whether the value can be determined by an expression:

<tag>
   ...
   <attribute>
      <name>attr1</name>
      <required>true|false|yes|no</required>
      <rtexprvalue>true|false|yes|no</rtexprvalue>
   </attribute>
</tag>
If a tag attribute is not required, a tag handler should provide a default value.


Attribute Validation

The documentation for a tag library should describe valid values for tag attributes. When a JSP page is translated, a JSP 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 isValid method of a class derived from TagExtraInfo. This class is also used to provide information about scripting variables defined by the tag (See "Tags That Define Scripting Variables").

The isValid method is passed the attribute information in a TagData object, 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 to TagData.REQUEST_TIME_VALUE.

The tag <tlt:twa attr1="value1" /> has the following TLD attribute element:

<attribute>
   <name>attr1</name>
   <required>true</required>
   <rtexprvalue>true</a>
</attribute
This declaration indicates that the value of attr1 can be determined at runtime.

The following isValid method checks that the value of attr1 is a valid boolean value. Note that since the value of attr1 can be computed at runtime, isValid must 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;
   }
}


Contents | Prev | Next
Copyright © 2000 Sun Microsystems, Inc. All rights reserved.