[ Team LiB ] Previous Section Next Section

Using Dynamic Attributes

In addition to using attributes that are declared beforehand, custom tags can also make use of attributes whose names are unknown before runtime. Although this might seem strange, it makes it possible to build tags that are flexible and easier to reuse. Tags that support this kind of behavior are known as dynamic attributes. They implement the DynamicAttributes interface, which consists of only one method:

public void setDynamicAttribute(String uri, String localName,
    Object Value ) throws JSPException

setDynamicAttribute is called when a tag that has declared that it accepts dynamic attributes is passed an attribute that has not been declared in the TLD. You state that a tag can accept dynamic attributes by adding the <tag> subelement:


The custom tag is responsible for storing the names and values of any dynamic attributes. Listing 16.11 illustrates one way that a tag could save the name and value of an attribute.

Listing 16.11 Partial Source Code for dynamic.java
private ArrayList names = new ArrayList();
private ArrayList values = new ArrayList();

public void setDynamicAttribute(String uri,
    String name, Object value ) throws JspException {
    names.add( name );
    values.add( value );
    [ Team LiB ] Previous Section Next Section