|
Download
FAQ History |
|
API
Search Feedback |
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:
AreaTagandMapTag. To see how the operations on a JavaServer Faces tag handler are implemented, let's take a look atbookstore6/src/taglib/MapTag: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
MapTagextendsUIComponentTag, which supportsjsp.tagext.Tagfunctionality as well as JavaServer Faces-specific functionality.UIComponentTagis 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
MapTagdoes is to retrieve the type of the component. It uses thegetComponentTypeoperation:Next, the tag handler sets the component's attribute values to those supplied as tag attributes in the page. The
MapTaghandler gets the attribute values from the page via JavaBeans properties that correspond to the attributes.MapComponenthas 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 thesetPropertiesmethod.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, and if you are updating a property of the underlying component 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 whyimmediateis of typeString, as shown in the code snippet.For each
bookstore6/src/components/MapComponentattribute that accepts a JavaServer Faces EL expression, thesetPropertiesmethod must get either aMethodBindingor aValueBindingfor it from theApplicationinstance. AValueBindingobject is used to evaluate value-binding expressions that refer to backing bean properties. AMethodBindingobject is used to evaluate reference expressions that refer to backing bean methods.For example, the value of the
actionListenerattribute must be a method-binding expression that points to a method on a backing bean that takes anActionEventas its argument. ThesetPropertiesmethod ofMapTagcreates aMethodBindingfor theactionListenerattribute, passing in the signature that this method must have, and it sets theMethodBindingas the value of theactionListenerattribute of theMapComponent.The
actionattribute can take a literalStringor 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, thesetPropertiesmethod creates a special constant method binding around the literalStringin order to satisfy the requirement that the argument to theactionattribute of theMapComponentbe aMethodBindinginstance. To handle the method-binding expression,setPropertiescreates theMethodBindingas it does for theactionListenerattribute.
MapComponent'simmediateattribute value is a value-binding expression. This expression points to a backing bean property. Therefore,setPropertiesmust obtain aValueBindingfor it. After obtaining theValueBinding, thesetPropertiesmethod sets the value of the property on theMapComponentby calling theMapComponent'ssetValueBindingmethod, passing in theValueBindingobtained from theApplicationand the name of the attribute.The following piece of
setPropertiessets theimmediateproperty 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
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.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.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.