|
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 lifecycle. For more information on the JavaServer Faces lifecycle, see The Lifecycle of a JavaServer Faces Page. The first thing that the tag handler does is 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 atMapTag:public class MapTag extends FacesTag { public String currentArea = null; public MapTag(){ super(); } public String getCurrentArea() { return currentArea; } public void setCurrentArea(String area) { currentArea = area; } public void overrideProperties(UIComponent component) { super.overrideProperties(component); UIMap map = (UIMap) component; if(map.getAttribute("currentArea") == null) map.setAttribute("currentArea", getCurrentArea()); } public String getRendererType() { return null; } public UIComponent createComponent() { return (new UIMap()); } } // end of classThe first thing to notice is that
MapTagextendsFacesTag, which supportsjsp.tagext.Tagfunctionality as well as JavaServer Faces-specific functionality.FacesTagis the base class for all JavaServer Faces tags that correspond to a component. Tags that need to process their tag bodies should subclassFacesBodyTaginstead.As explained above, the first thing
MapTagdoes is to retrieve the type of the component. This is done with 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.UIMaponly has one attribute,currentArea. Here is the property used to access the value ofcurrentArea:public String currentArea = null; ... public String getCurrentArea() {return currentArea;} public void setCurrentArea(String area) { currentArea = area; }To pass the value of
currentAreato theUIMapcomponent, the tag handler implements theoverridePropertiesmethod, which calls theUIMap.setAttributemethod with the name and value ofcurrentAreaattribute:public void overrideProperties(UIComponent component) { super.overrideProperties(component); UIMap map = (UIMap) component; if(map.getAttribute("currentArea") == null) map.setAttribute("currentArea", getCurrentArea()); }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 with the
getRendererTypemethod:Since
UIMapdoes not have a renderer associated with it, this method returns null. In this case, the JavaServer Faces implementation will invoke the encoding methods ofUIMapto perform the rendering.Delegating Rendering to a Renderer provides an example of returning a renderer from this method.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.