background image

Setting Deferred Value Attributes

<< Setting Dynamic Attributes | Tag Handlers for Tags with Bodies >>
<< Setting Dynamic Attributes | Tag Handlers for Tags with Bodies >>

Setting Deferred Value Attributes

The following implementation of setDynamicAttribute saves the attribute names and values
in lists. Then, in the doTag method, the names and values are echoed to the response in an
HTML list.
private ArrayList keys = new ArrayList();
private ArrayList values = new ArrayList();
public void setDynamicAttribute(String uri,
String localName, Object value ) throws JspException {
keys.add( localName );
values.add( value );
}
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
for( int i = 0; i < keys.size(); i++ ) {
String key = (String)keys.get( i );
Object value = values.get( i );
out.println(
"<li>" + key + " = " + value + "</li>" );
}
}
Setting Deferred Value Attributes and Deferred Method Attributes
For each tag attribute that accepts a deferred value expression or a deferred method expression,
the tag handler must have a method to access the value of the attribute.
The methods that access the value of a deferred value attribute method must accept a
ValueExpression
object. The methods that access the value of a deferred method attribute
must accept a MethodExpression object. These methods take the form setXXX, where XXX is
the name of the attribute.
The following example shows a method that can be used to access the value of a deferred value
attribute called attributeName:
private javax.el.ValueExpression attributeName = null;
public void setAttributeName(
javax.el.ValueExpression attributeName)
{
this.attributeName = attributeName;
}
Deferred value attributes and deferred method attributes are primarily used by JavaServer Faces
technology. See
"Getting the Attribute Values" on page 431
for an example of creating a tag
handler that processes these attributes for a JavaServer Faces application.
Programming Simple Tag Handlers
Chapter 8 · Custom Tags in JSP Pages
259