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:
AreaTagandMapTag. To see how the operations on a JavaServer Faces tag handler are implemented, let's take a look atMapTag.The
MapTagclass extendsUIComponentELTag, which supportsjsp.tagext.Tagfunctionality as well as JavaServer Faces-specific functionality.UIComponentELTagis 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
MapTagdoes is to retrieve the type of the component. It does this by using thegetComponentTypeoperation:The value returned from
getComponentTypemust match the value configured for the component with thecomponent-typeelement of the application's application configuration resource file. Registering a Custom Component (page 471) 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
MapTaghandler 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 theimmediateattribute.private javax.el.ValueExpression immediate = null; public void setImmediate(javax.el.ValueExpression immediate) { this.immediate = immediate; }As this code shows, the
setImmediatemethod takes aValueExpressionobject. This means that theimmediateattribute of themaptag accepts value expressions.Similarly, the
setActionListenerandsetActionmethods takeMethodExpressionobjects, which means that these attributes accept method expressions. The following code shows the properties used to access the values of theactionListenerand theactionattributesprivate 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 thesetPropertiesmethod. The waysetPropertiespasses 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,
setPropertiesfirst checks if it is not a literal expression. If the expression is not a literal,setPropertiesstores 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,setPropertiesperforms 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
setPropertiescalls the corresponding setter method of the component class.- If the attribute is renderer-dependent then
setPropertiesstores the converted value into the component's map of generic renderer attributes.The following piece of the
MapTaghandler'ssetPropertiesmethod 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
actionListenerattribute uses a method expression to reference a method that handles action events. Theactionattribute 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, thesetPropertiesmethod 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. ThesetPropertiesmethod then adds thisMethodExpressionActionListenerobject to the list of listeners to be notified when the event of a user clicking on the map occurs. The following piece ofsetPropertiesdoes all of this:if (actionListener != null) { map.addActionListener( new MethodExpressionActionListener(actionListener)); }If your component fires value change events, your tag handler's
setPropertiesmethod does a similar thing, except it wraps the expression in aMethodExpressionValueChangeListenerobject and adds the listener using theaddValueChangeListenermethod.In the case of the method expression referenced by the
actionattribute, thesetPropertiesmethod uses thesetActionExpressionmethod ofActionSource2to 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
getRendererTypemethod: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,
getRendererTypeshould returnnull. In this case, therenderer-typeelement in the application configuration file should also be set tonull.Releasing Resources
It's recommended practice that all tag handlers implement a
releasemethod, which releases resources allocated during the execution of the tag handler. The release method ofMapTagas follows:public void release() { super.release(); current = null; styleClass = null; actionListener = null; immediate = null; action = null; }This method first calls the
UIComponentTag.releasemethod to release resources associated withUIComponentTag. Next, the method sets all attribute values tonull.