background image

Delegating Rendering to a Renderer

<< Saving and Restoring State | The encodeEnd Method >>
<< Saving and Restoring State | The encodeEnd Method >>

Delegating Rendering to a Renderer

To specify where state is saved for a particular web application, you need to set the
javax.faces.STATE_SAVING_METHOD
context parameter to either client or server in your
application's deployment descriptor. See
"Saving and Restoring State" on page 426
for more
information on specifying where state is saved in the deployment descriptor.
Delegating Rendering to a Renderer
Both MapComponent and AreaComponent delegate all of their rendering to a separate renderer.
The section
"Performing Encoding" on page 422
explains how MapRenderer performs the
encoding for MapComponent. This section explains in detail the process of delegating rendering
to a renderer using AreaRenderer, which performs the rendering for AreaComponent.
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" on page 455
).
Identify the renderer type in the component's tag handler.
Creating 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
delegates encoding to the AreaRenderer class.
To perform the rendering for AreaComponent, AreaRenderer must implement an encodeEnd
method. The encodeEnd method of AreaRenderer retrieves the shape, coordinates, and
alternative text values stored in the ImageArea bean that is bound to AreaComponent. Suppose
that the area tag currently being rendered has a value attribute value of "fraA". The following
line from encodeEnd gets the value of the attribute "fraA" from the FacesContext instance.
ImageArea ia = (ImageArea)area.getValue();
The attribute value is the ImageArea bean instance, which contains the shape, coords, and alt
values associated with the fraA AreaComponent instance.
"Configuring Model Data" on
page 416
describes how the application stores these values.
After retrieving the ImageArea object, it renders the values for shape, coords, and alt by
simply calling the associated accessor methods and passing the returned values to the
ResponseWriter
instance, as shown by these lines of code, which write out the shape and
coordinates:
writer.startElement(
"area", area);
writer.writeAttribute(
"alt", iarea.getAlt(), "alt");
Delegating Rendering to a Renderer
Chapter 13 · Creating Custom UI Components
427