background image

Performing Encoding

<< Specifying the Component Family | The Encoding Methods >>
<< Specifying the Component Family | The Encoding Methods >>

Performing Encoding

Performing Encoding
During the render response phase, the JavaServer Faces implementation processes the encoding
methods of all components and their associated renderers in the view. The encoding methods
convert the current local value of the component into the corresponding markup that
represents it in the response.
The UIComponentBase class defines a set of methods for rendering markup: encodeBegin,
encodeChildren
, and encodeEnd. If the component has child components, you might need to
use more than one of these methods to render the component; otherwise, all rendering should
be done in encodeEnd.
Because MapComponent is a parent component of AreaComponent, the area tags must be
rendered after the beginning map tag and before the ending map tag. To accomplish this, the
MapRenderer
class renders the beginning map tag in encodeBegin and the rest of the map tag in
encodeEnd
.
The JavaServer Faces implementation automatically invokes the encodeEnd method of
AreaComponent
's renderer after it invokes MapRenderer's encodeBegin method and before it
invokes MapRenderer's encodeEnd method. If a component needs to perform the rendering for
its children, it does this in the encodeChildren method.
Here are the encodeBegin and encodeEnd methods of MapRenderer:
public void encodeBegin(FacesContext context,
UIComponent component) throws IOException {
if ((context == null)|| (component == null)){
throw new NullPointerException();
}
MapComponent map = (MapComponent) component;
ResponseWriter writer = context.getResponseWriter();
writer.startElement(
"map", map);
writer.writeAttribute(
"name", map.getId(),"id");
}
public void encodeEnd(FacesContext context) throws IOException {
if ((context == null) || (component == null)){
throw new NullPointerException();
}
MapComponent map = (MapComponent) component;
ResponseWriter writer = context.getResponseWriter();
writer.startElement(
"input", map);
writer.writeAttribute(
"type", "hidden", null);
writer.writeAttribute(
"name",
getName(context,map),
"clientId");(
writer.endElement(
"input");
writer.endElement(
"map");
}
Creating Custom Component Classes
The Java EE 5 Tutorial · September 2007
422