Creating the Component Tag Handler
If you've created your own JSP custom tags before, creating a component tag and 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.
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. Finally, it 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.
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
:public class MapTag extends UIComponentTag { private String current = null; public void setCurrent(String current) { this.current = current; } private String actionListener = null; public void setActionListener(String actionListener) { this.actionListener = actionListener; } private String action = null; public void setAction(String action) { this.action = action; } private String immediate = null; public void setImmediate(String immediate) { this.immediate = immediate; } private String styleClass = null; public void setStyleClass(String styleClass) { this.styleClass = styleClass; } public String getComponentType() { return ("DemoMap"); } public String getRendererType() { return ("DemoMap"); } public void release() { super.release(); current = null; styleClass = null; actionListener = null; action = null; immediate = null; } protected void setProperties(UIComponent component) { super.setProperties(component); MapComponent map = (MapComponent) component; if (styleClass != null) { if (isValueReference(styleClass)) { ValueBinding vb = FacesContext.getCurrentInstance(). getApplication(). createValueBinding(styleClass); map.setValueBinding("styleClass", vb); } else { map.getAttributes().put("styleClass", styleClass); } } if(actionListener != null) { if(isValueReference(actionListener)) { Class args[] = {ActionEvent.class}; MethodBinding mb = FacesContext.getCurrentInstance(). getApplication(). createMethodBinding(actionListener, args); map.setActionListener(mb); } else { Object params[] = {actionListener}; throw new javax.faces.FacesException(); } } if (action != null) { if (isValueReference(action)) { MethodBinding vb = FacesContext. getCurrentInstance().getApplication(). createMethodBinding(action, null); map.setAction(vb); } else { map.setAction( Util.createConstantMethodBinding(action)); } } if (immediate != null) { if (isValueReference(immediate)) { ValueBinding vb = FacesContext. getCurrentInstance().getApplication(). createValueBinding(immediate); map.setValueBinding("immediate", vb); } else { boolean _immediate = new Boolean(immediate).booleanValue(); map.setImmediate(_immediate); } } }The first thing to notice is that
MapTag
extendsUIComponentTag
, which supportsjsp.tagext.Tag
functionality as well as JavaServer Faces-specific functionality.UIComponentTag
is the base class for all JavaServer Faces tags that correspond to a component. Tags that need to process their tag bodies should instead subclassUIComponentBodyTag
.As explained earlier, the first thing
MapTag
does is to retrieve the type of the component. It uses thegetComponentType
operation to do this: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 explains how to configure a component.Next, the tag handler sets the component's attribute values to those supplied as tag attributes in the page. The
MapTag
handler gets the attribute values from the page via JavaBeans properties that correspond to the attributes.MapComponent
has several attributes. Here is the property that is used to access the value ofimmediate
:private String immediate = null; public void setImmediate(String immediate) { this.immediate = immediate; }To pass the value of the tag attributes to
MapComponent
, the tag handler implements thesetProperties
method.Some tag attributes can refer to literal values or use value-binding expressions, which point to values typically stored in a bean. It is recommended that you enable your component attributes to accept value-binding expressions because this is what a page author expects.
If you do make your tag attributes accept value-binding expressions then the component property must also be enabled for value-binding expressions. See Enabling Value-Binding of Component Properties for more information. In addition, an attribute that accepts a value-binding expression must be of type
String
. This is whyimmediate
is of typeString
, as shown in the preceding code snippet.For each
MapComponent
attribute that accepts a JavaServer Faces EL expression, thesetProperties
method must get either aMethodBinding
or aValueBinding
for it from theApplication
instance. AValueBinding
object is used to evaluate value-binding expressions that refer to backing bean properties. AMethodBinding
object is used to evaluate method-binding expressions that refer to backing bean methods.For example, the value of the
actionListener
attribute must be a method-binding expression that points to a method on a backing bean that takes anActionEvent
object as its argument. ThesetProperties
method ofMapTag
creates aMethodBinding
for theactionListener
attribute, passing in the signature that this method must have, and it sets theMethodBinding
object as the value of theactionListener
attribute ofMapComponent
.The
action
attribute can take a literalString
or a method-binding expression that points to a backing bean method that takes no parameters and returns a literalString
. To handle the case of the literalString
, thesetProperties
method creates a special constant method binding around the literalString
in order to satisfy the requirement that the argument to theaction
attribute ofMapComponent
be aMethodBinding
instance. To handle the method-binding expression,setProperties
creates theMethodBinding
object as it does for theactionListener
attribute.The
MapComponent
object'simmediate
attribute value is a value-binding expression. This expression points to a backing bean property. Therefore,setProperties
must obtain aValueBinding
instance for it. After obtaining theValueBinding
instance, thesetProperties
method sets the value of the property onMapComponent
by calling theMapComponent
class'ssetValueBinding
method, passing in theValueBinding
instance obtained from theApplication
and the name of the attribute.The following piece of
setProperties
sets theimmediate
property ofMapComponent
:... if (immediate != null) { if (isValueReference(immediate)) { ValueBinding vb = FacesContext. getCurrentInstance().getApplication(). createValueBinding(immediate); map.setValueBinding("immediate", vb); } else { boolean _immediate = new Boolean(immediate).booleanValue(); map.setImmediate(_immediate); } }Finally, 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
.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
.