Creating the Component Tag Handler


Now that you've created your component and renderer classes, you're ready to define how a tag handler processes the tag representing the component and renderer combination. If you've created your own JSP custom tags before, creating a component tag handler should be easy for you.

In JavaServer Faces applications, the tag handler class associated with a component drives the render response phase of the JavaServer Faces life cycle. For more information on the JavaServer Faces life cycle, see The Life Cycle of a JavaServer Faces Page (page 309).

The first thing that the tag handler does is to retrieve the type of the component associated with the tag. Next, it sets the component's attributes to the values given in the page. It then returns the type of the renderer (if there is one) to the JavaServer Faces implementation so that the component's encoding can be performed when the tag is processed. Finally, it releases resources used during the processing of the tag.

The image map custom component includes two tag handlers: AreaTag and MapTag. To see how the operations on a JavaServer Faces tag handler are implemented, let's take a look at MapTag.

The MapTag class extends UIComponentELTag, which supports jsp.tagext.Tag functionality as well as JavaServer Faces-specific functionality. UIComponentELTag is the base class for all JavaServer Faces tags that correspond to a component. Tags that need to process their tag bodies should instead subclass UIComponentBodyELTag.

Retrieving the Component Type

As explained earlier, the first thing MapTag does is to retrieve the type of the component. It does this by using the getComponentType operation:

   public String getComponentType() {      return ("DemoMap");    }


The value returned from getComponentType must match the value configured for the component with the component-type element of the application's application configuration resource file. Registering a Custom Component (page 480) explains how to configure a component.

Setting Component Property Values

After retrieving the type of the component, the tag handler sets the component's property values to those supplied as tag attributes values in the page. This section assumes that your component properties are enabled to accept expressions, as explained in Enabling Component Properties to Accept Expressions (page 443).

Getting the Attribute Values

Before setting the values in the component class, the MapTag handler first gets the attribute values from the page via JavaBeans component properties that correspond to the attributes. The following code shows the property used to access the value of the immediate attribute.

      private javax.el.ValueExpression immediate = null;       public void setImmediate(javax.el.ValueExpression immediate)       {         this.immediate = immediate;       }


As this code shows, the setImmediate method takes a ValueExpression object. This means that the immediate attribute of the map tag accepts value expressions.

Similarly, the setActionListener and setAction methods take MethodExpression objects, which means that these attributes accept method expressions. The following code shows the properties used to access the values of the actionListener and the action attributes

   private javax.el.MethodExpression actionListener = null;    public void setActionListener(      javax.el.MethodExpression actionListener) {      this.actionListener = actionListener;    }    private javax.el.MethodExpression action = null;    public void setAction(javax.el.MethodExpression action) {        this.action = action;    }


Setting the Component Property Values

To pass the value of the tag attributes to MapComponent, the tag handler implements the setProperties method. The way setProperties passes the attribute values to the component class depends on whether the values are value expressions or method expressions.

Setting Value Expressions on Component Properties

When the attribute value is a value expression, setProperties first checks if it is not a literal expression. If the expression is not a literal, setProperties stores the expression into a collection, from which the component class can retrieve it and resolve it at the appropriate time. If the expression is a literal, setProperties performs any required type conversion and then does one of the following:

  • If the attribute is renderer-independent, meaning that it is defined by the component class, then setProperties calls the corresponding setter method of the component class.

  • If the attribute is renderer-dependent, setProperties stores the converted value into the component's map of generic renderer attributes.

The following piece of the MapTag handler's setProperties method sets the renderer-dependent property, styleClass, and the renderer-independent property, immediate:

   if (styleClass != null) {      if (!styleClass.isLiteralText()) {        map.setValueExpression("styleClass", styleClass);      } else {        map.getAttributes().put("styleClass",          styleClass.getExpressionString());      }    }    ...    if (immediate != null) {      if (!immediate.isLiteralText()) {        map.setValueExpression("immediate", immediate);      } else {        map.setImmediate(new          Boolean(immediate.getExpressionString()).            booleanValue());      }    }


Setting Method Expressions on Component Properties

The process of setting the properties that accept method expressions is done differently depending on the purpose of the method. The actionListener attribute uses a method expression to reference a method that handles action events. The action attribute uses a method expression to either specify a logical outcome or to reference a method that returns a logical outcome, which is used for navigation purposes.

To handle the method expression referenced by actionListener, the setProperties method must wrap the expression in a special action listener object called MethodExpressionActionListener. This listener executes the method referenced by the expression when it receives the action event. The setProperties method then adds this MethodExpressionActionListener object to the list of listeners to be notified when the event of a user clicking on the map occurs. The following piece of setProperties does all of this:

   if (actionListener != null) {      map.addActionListener(        new MethodExpressionActionListener(actionListener));    }


If your component fires value change events, your tag handler's setProperties method does a similar thing, except it wraps the expression in a MethodExpressionValueChangeListener object and adds the listener using the addValueChangeListener method.

In the case of the method expression referenced by the action attribute, the setProperties method uses the setActionExpression method of ActionSource2 to set the corresponding property on MapComponent:

   if (action != null) {      map.setActionExpression(action);    }


Providing the Renderer Type

After setting the component properties, the tag handler provides a renderer typeif there is a renderer associated with the componentto the JavaServer Faces implementation. It does this using the getrendererType method:

   public String getRendererType() {return "DemoMap";}


The renderer type that is returned is the name under which the renderer is registered with the application. See Delegating Rendering to a Renderer (page 446) for more information.

If your component does not have a renderer associated with it, getrendererType should return null. In this case, the renderer-type element in the application configuration file should also be set to null.

Releasing Resources

It's recommended practice that all tag handlers implement a release method, which releases resources allocated during the execution of the tag handler. The release method of MapTag as follows:

   public void release() {      super.release();      current = null;      styleClass = null;      actionListener = null;      immediate = null;      action = null;    }


This method first calls the UIComponentTag.release method to release resources associated with UIComponentTag. Next, the method sets all attribute values to null.



The JavaT EE 5 Tutorial
The JavaT EE 5 Tutorial
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 309

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net