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 Handling Events for Custom Components Creating the Component Tag Handler Setting Component Property Values Setting the 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 | Delegating Rendering to a RendererBothMapComponent andAreaComponent delegate all of their rendering to a separate renderer.The sectionPerforming Encoding explains howMapRenderer performs the encoding forMapComponent. This sectionexplains in detail the process of delegating rendering to a renderer usingAreaRenderer,which performs the rendering forAreaComponent. To delegate rendering, you perform these tasks:
Creating the Renderer ClassWhen delegating rendering to a renderer, you can delegate all encoding and decodingto the renderer, or you can choose to do part of itin the component class. TheAreaComponent class delegates encoding to theAreaRenderer class. To perform the rendering forAreaComponent,AreaRenderer must implement anencodeEnd method. TheencodeEndmethod ofAreaRenderer retrieves the shape, coordinates, and alternative text values stored in theImageArea bean that is bound toAreaComponent. Suppose that thearea tag currently beingrendered has avalue attribute value of"fraA". The following line fromencodeEnd gets the value of the attribute"fraA" from theFacesContext instance. ImageArea ia = (ImageArea)area.getValue(); The attribute value is theImageArea bean instance, which contains theshape,coords,andalt values associated with thefraAAreaComponent instance.Configuring Model Data describes how theapplication stores these values. After retrieving theImageArea object, it renders the values forshape,coords, andaltby simply calling the associated accessor methods and passing the returned values totheResponseWriter instance, as shown by these lines of code, which write outthe shape and coordinates: writer.startElement("area", area);writer.writeAttribute("alt", iarea.getAlt(), "alt");writer.writeAttribute("coords", iarea.getCoords(), "coords");writer.writeAttribute("shape", iarea.getShape(), "shape");TheencodeEnd method also renders the JavaScript for theonmouseout,onmouseover, andonclickattributes. The page author need only provide the path to the images thatare to be loaded during anonmouseover oronmouseout action: <bookstore:area value="#{fraA}" onmouseover="/template/world_france.jpg" onmouseout="/template/world.jpg" targetImage="mapImage" />TheAreaRenderer class takes care of generating the JavaScript for these actions, asshown in the following code fromencodeEnd. The JavaScript thatAreaRenderer generates fortheonclick action sets the value of the hidden field to the valueof the current area’s component ID and submits the page. sb = new StringBuffer("document.forms[0][’"). append(targetImageId).append("’].src=’");sb.append(getURI(context, (String) area.getAttributes().get("onmouseout")));sb.append("’");writer.writeAttribute("onmouseout", sb.toString(), "onmouseout");sb = new StringBuffer("document.forms[0][’"). append(targetImageId).append("’].src=’");sb.append(getURI(context, (String) area.getAttributes().get("onmouseover")));sb.append("’");writer.writeAttribute("onmouseover", sb.toString(), "onmouseover");sb = new StringBuffer("document.forms[0][’");sb.append(getName(context, area));sb.append("’].value=’");sb.append(iarea.getAlt());sb.append("’; document.forms[0].submit()");writer.writeAttribute("onclick", sb.toString(), "value");writer.endElement("area");By submitting the page, this code causes the JavaServer Faces life cycle toreturn back to the restore view phase. This phase saves any state information,including the value of the hidden field, so that a new request componenttree is constructed. This value is retrieved by thedecode method of theMapComponent class. Thisdecode method is called by the JavaServer Faces implementation duringthe apply request values phase, which follows the restore view phase. In addition to theencodeEnd method,AreaRenderer contains an empty constructor. Thisis used to create an instance ofAreaRenderer so that it can beadded to the render kit. Identifying the Renderer TypeDuring the render response phase, the JavaServer Faces implementation calls thegetRendererType methodof the component’s tag handler to determine which renderer to invoke, if thereis one. ThegetRendererType method ofAreaTag must return the type associated withAreaRenderer. Youidentify this type when you registerAreaRenderer with the render kit, as described inRegistering a Custom Renderer with a Render Kit. Here is thegetRendererType method from theAreaTag class: public String getRendererType() { return ("DemoArea");}Creating the Component Tag Handler explains more about thegetRendererType method. Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |