Download
FAQ History |
API
Search Feedback |
Delegating Rendering to a Renderer
Both the
MapComponent
and theAreaComponent
delegate all of their rendering to a separate renderer. The section Performing Encoding explains how theMapRenderer
performs the encoding for theMapComponent
. This section explains in detail the process of delegating rendering to a renderer using theAreaRenderer
, which performs the rendering forAreaComponent
.To delegate rendering, you perform these tasks:
- Create the
Renderer
class- Register the renderer with a render kit (explained in Registering a Custom Renderer with a Render Kit)
- Identify the renderer type in the component's tag handler
Create the Renderer Class
When delegating rendering to a renderer, you can delegate all encoding and decoding to the renderer, or you can choose to do part of it in the component class. The
AreaComponent
class requires only encoding.To perform the rendering for
AreaComponent
, the AreaRenderer must implement anencodeEnd
method. TheencodeEnd
method ofAreaRenderer
must retrieve the shape, coordinates, and alt values stored in theImageArea
bean that is bound toAreaComponent
. Suppose that thearea
tag currently being rendered has avalue
attribute value of"fraA"
. The following line fromencodeEnd
gets the value of the attribute"fraA"
from the FacesContext.The attribute value is the
ImageArea
bean instance, which contains the shape, coordinates, and alt values associated with thefraA
AreaComponent
instance. Configuring Model Data describes how the application stores these values.After retrieving the
ImageArea
object, it renders the values forshape
,coords
, andalt
by simply calling the associated accessor methods and passing the returned values to theResponseWriter
, as shown by these lines of code, which write out the 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");The
encodeEnd
method also renders the JavaScript for theonmouseout
,onmouseover
, andonclick
attributes. The page author need only provide the path to the images that are to be loaded during anonmouseover
oronmouseout
action:<d:area id="France" value="#{fraA}" onmouseover="/template/world_france.jpg" onmouseout="/template/world.jpg" targetImage="mapImage" />The
AreaRenderer
class takes care of generating the JavaScript for these actions, as shown in the following code fromencodeEnd
. The JavaScript thatAreaRenderer
generates for theonclick
action sets the value of the hidden field to the value of 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 to return back to the restore view phase. This phase saves any state information--including the value of the hidden field--so that a new request component tree is constructed. This value is retrieved by the
decode
method of theMapComponent
class. Thisdecode
method is called by the JavaServer Faces implementation during the apply request values phase, which follows the restore view phase.In addition to the
encodeEnd
method,AreaRenderer
contains an empty constructor. This is used to create an instance ofAreaRenderer
so that it can be added to the render kit.Note that
AreaRenderer
extendsBaseRenderer
, which in turn extendsRenderer
. It contains definitions of theRenderer
class methods so that you don't have to include them in your renderer class.Identify the Renderer Type
During the render response phase, the JavaServer Faces implementation calls the
getRendererType
method of the component's tag to determine which renderer to invoke, if there is one.The
getRendererType
method ofbookstore6/src/taglib/AreaTag
must return the type associated withAreaRenderer
. Recall that you identified this type when you registeredAreaRenderer
with the render kit. Here is thegetRendererType
method from the Duke Bookstore application'sAreaTag
class:
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.