2. Using the Tutorial Examples 3. Getting Started with Web Applications 5. JavaServer Pages Technology 7. JavaServer Pages Standard Tag Library 10. JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages 12. Developing with JavaServer Faces Technology 13. Creating Custom UI Components Determining Whether You Need a Custom Component or Renderer When to Use a Custom Component Component, Renderer, and Tag Combinations Understanding the Image Map Example Why Use JavaServer Faces Technology to Implement an Image Map? Understanding the Rendered HTML Summary of the Application Classes Steps for Creating a Custom Component Creating Custom Component Classes Specifying the Component Family Enabling Component Properties to Accept Expressions Delegating Rendering to a Renderer Handling Events for Custom Components Creating the Component Tag Handler Setting Component Property Values Defining the Custom Component Tag in a Tag Library Descriptor 14. Configuring JavaServer Faces Applications 15. Internationalizing and Localizing Web Applications 16. Building Web Services with JAX-WS 17. Binding between XML Schema and Java Classes 19. SOAP with Attachments API for Java 21. Getting Started with Enterprise Beans 23. A Message-Driven Bean Example 24. Introduction to the Java Persistence API 25. Persistence in the Web Tier 26. Persistence in the EJB Tier 27. The Java Persistence Query Language 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API 36. The Coffee Break Application | Creating the Component Tag HandlerNow that you’ve created your component and renderer classes, you’re ready to definehow 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 taghandler should be easy for you. In JavaServer Faces applications, the tag handler class associated with a component drivesthe render response phase of the JavaServer Faces life cycle. For more informationon the JavaServer Faces life cycle, seeThe Life Cycle of a JavaServer Faces Page. The first thing that the tag handler does is to retrieve thetype of the component associated with the tag. Next, it sets the component’s attributesto the values given in the page. It then returns the typeof the renderer (if there is one) to the JavaServer Faces implementation sothat the component’s encoding can be performed when the tag is processed. Finally, itreleases 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. TheMapTag class extendsUIComponentELTag, which supportsjsp.tagext.Tag functionality as well as JavaServerFaces-specific functionality.UIComponentELTag is the base class for all JavaServer Faces tags thatcorrespond to a component. Tags that need to process their tag bodies shouldinstead subclassUIComponentBodyELTag. Retrieving the Component TypeAs explained earlier, the first thingMapTag does is to retrieve thetype of the component. It does this by using thegetComponentType operation: public String getComponentType() { return ("DemoMap"); }The value returned fromgetComponentType must match the value configured for the componentwith thecomponent-type element of the application’s application configuration resource file.Registering a Custom Component explainshow to configure a component. Setting Component Property ValuesAfter retrieving the type of the component, the tag handler sets the component’sproperty values to those supplied as tag attributes values in the page. Thissection assumes that your component properties are enabled to accept expressions, as explainedinEnabling Component Properties to Accept Expressions. Getting the Attribute ValuesBefore setting the values in the component class, theMapTag handler first getsthe attribute values from the page by means of JavaBeans component properties thatcorrespond to the attributes. The following code shows the property used to accessthe value of theimmediate attribute. private javax.el.ValueExpression immediate = null;public void setImmediate(javax.el.ValueExpression immediate) { this.immediate = immediate;}As this code shows, thesetImmediate method takes aValueExpression object. This meansthat theimmediate attribute of themap tag accepts value expressions. Similarly, thesetActionListener andsetAction methods takeMethodExpression objects, which means that theseattributes accept method expressions. The following code shows the properties used to access thevalues of theactionListener and theaction attributes private 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 ValuesTo pass the value of the tag attributes toMapComponent, the tag handlerimplements thesetProperties method. The waysetProperties passes the attribute values to thecomponent class depends on whether the values are value expressions or method expressions. Setting Value Expressions on Component PropertiesWhen the attribute value is a value expression,setProperties first checks if itis not a literal expression. If the expression is not a literal,setProperties stores the expression into a collection, from which the component class canretrieve it and resolve it at the appropriate time. If the expression isa literal,setProperties performs any required type conversion and then does one ofthe following:
The following piece of theMapTag handler’ssetProperties method sets the renderer-dependentproperty,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 PropertiesThe process of setting the properties that accept method expressions is done differentlydepending on the purpose of the method. TheactionListener attribute uses amethod expression to reference a method that handles action events. Theaction attributeuses a method expression to either specify a logical outcome or to referencea method that returns a logical outcome, which is used for navigation purposes. To handle the method expression referenced byactionListener, thesetProperties method must wrapthe expression in a special action listener object calledMethodExpressionActionListener. This listener executes themethod referenced by the expression when it receives the action event. ThesetPropertiesmethod then adds thisMethodExpressionActionListener object to the list of listeners to benotified 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’ssetProperties methoddoes a similar thing, except it wraps the expression in aMethodExpressionValueChangeListener objectand adds the listener using theaddValueChangeListener method. In the case of the method expression referenced by theaction attribute, thesetProperties method uses thesetActionExpression method ofActionSource2 to set the corresponding propertyonMapComponent: if (action != null) { map.setActionExpression(action);}Providing the Renderer TypeAfter setting the component properties, the tag handler provides a renderer type (ifthere is a renderer associated with the component) to the JavaServer Faces implementation.It does this using thegetRendererType method: public String getRendererType() {return "DemoMap";}The renderer type that is returned is the name under which therenderer is registered with the application. SeeDelegating Rendering to a Renderer for more information. If your component does not have a renderer associated with it,getRendererTypeshould returnnull. In this case, therenderer-type element in the applicationconfiguration file should also be set tonull. Releasing ResourcesIt’s recommended practice that all tag handlers implement arelease method, which releasesresources 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 theUIComponentTag.release method to release resources associated withUIComponentTag. Next, the method sets all attribute values tonull. Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |