background image

Setting Dynamic Attributes

<< Tag Handlers for Basic Tags | Setting Deferred Value Attributes >>
<< Tag Handlers for Basic Tags | Setting Deferred Value Attributes >>

Setting Dynamic Attributes

The validate method is passed the attribute information in a TagData object, which contains
attribute-value tuples for each of the tag's attributes. Because 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 <tt:twa attr1="value1"/> has the following TLD attribute element:
<attribute>
<name>attr1</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
This declaration indicates that the value of attr1 can be determined at runtime.
The following validate method checks whether the value of attr1 is a valid Boolean value.
Note that because the value of attr1 can be computed at runtime, validate must check
whether the tag user has chosen to provide a runtime value.
public class TwaTEI extends TagExtraInfo {
public ValidationMessage[] validate(TagData data) {
Object o = data.getAttribute(
"attr1");
if (o != null && o != TagData.REQUEST_TIME_VALUE) {
if (((String)o).toLowerCase().equals(
"true") ||
((String)o).toLowerCase().equals(
"false") )
return null;
else
return new ValidationMessage(data.getId(),
"Invalid boolean value.");
}
else
return null;
}
}
Setting Dynamic Attributes
Simple tag handlers that support dynamic attributes must declare that they do so in the tag
element of the TLD (see
"Declaring Tag Handlers" on page 251
). In addition, your tag handler
must implement the setDynamicAttribute method of the DynamicAttributes interface. For
each attribute specified in the tag invocation that does not have a corresponding attribute
element in the TLD, the web container calls setDynamicAttribute, passing in the namespace of
the attribute (or null if in the default namespace), the name of the attribute, and the value of the
attribute. You must implement the setDynamicAttribute method to remember the names and
values of the dynamic attributes so that they can be used later when doTag is executed. If the
setDynamicAttribute
method throws an exception, the doTag method is not invoked for the
tag, and the exception must be treated in the same manner as if it came from an attribute setter
method.
Programming Simple Tag Handlers
The Java EE 5 Tutorial · September 2007
258