background image

The encodeEnd Method

<< Delegating Rendering to a Renderer | Handling Events for Custom Components >>
<< Delegating Rendering to a Renderer | Handling Events for Custom Components >>

The encodeEnd Method

writer.writeAttribute(
"coords", iarea.getCoords(), "coords");
writer.writeAttribute(
"shape", iarea.getShape(), "shape");
The encodeEnd method also renders the JavaScript for the onmouseout, onmouseover, and
onclick
attributes. The page author need only provide the path to the images that are to be
loaded during an onmouseover or onmouseout action:
<bookstore: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 from encodeEnd. The JavaScript that AreaRenderer generates for the onclick
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 the MapComponent class. This decode 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 of AreaRenderer so that it can be added to the render kit.
Delegating Rendering to a Renderer
The Java EE 5 Tutorial · September 2007
428