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 300).
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
andMapTag
. To see how the operations on a JavaServer Faces tag handler are implemented, let's take a look atMapTag
.The
MapTag
class extendsUIComponentELTag
, which supportsjsp.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 subclassUIComponentBodyELTag
.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 thegetComponentType
operation:The value returned from
getComponentType
must match the value configured for the component with thecomponent-type
element of the application's application configuration resource file. Registering a Custom Component (page 469) 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.
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 theimmediate
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 aValueExpression
object. This means that theimmediate
attribute of themap
tag accepts value expressions.Similarly, the
setActionListener
andsetAction
methods takeMethodExpression
objects, which means that these attributes accept method expressions. The following code shows the properties used to access the values of theactionListener
and theaction
attributesprivate 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 thesetProperties
method. The waysetProperties
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 then
setProperties
stores the converted value into the component's map of generic renderer attributes.The following piece of the
MapTag
handler'ssetProperties
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. Theaction
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
, thesetProperties
method must wrap the expression in a special action listener object calledMethodExpressionActionListener
. This listener executes the method referenced by the expression when it receives the action event. ThesetProperties
method then adds thisMethodExpressionActionListener
object to the list of listeners to be notified when the event of a user clicking on the map occurs. The following piece ofsetProperties
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 aMethodExpressionValueChangeListener
object and adds the listener using theaddValueChangeListener
method.In the case of the method expression referenced by the
action
attribute, thesetProperties
method uses thesetActionExpression
method ofActionSource2
to set the corresponding property onMapComponent
:Providing the Renderer Type
After setting the component properties, the tag handler provides a renderer type--if there is a renderer associated with the component--to the JavaServer Faces implementation. It does this using the
getRendererType
method:The renderer type that is returned is the name under which the renderer is registered with the application. See Delegating Rendering to a Renderer for more information.
If your component does not have a renderer associated with it,
getRendererType
should returnnull
. In this case, therenderer-type
element in the application configuration file should also be set tonull
.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 ofMapTag
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 withUIComponentTag
. Next, the method sets all attribute values tonull
.