10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating Custom Component Classes<br />

420<br />

Performing Encoding<br />

During the render response phase, the <strong>Java</strong>Server Faces implementation processes the encoding<br />

methods of all components and their associated renderers in the view. <strong>The</strong> encoding methods<br />

convert the current local value of the component into the corresponding markup that<br />

represents it in the response.<br />

<strong>The</strong> UIComponentBase class defines a set of methods for rendering markup: encodeBegin,<br />

encodeChildren, and encodeEnd. If the component has child components, you might need to<br />

use more than one of these methods to render the component; otherwise, all rendering should<br />

be done in encodeEnd.<br />

Because MapComponent is a parent component of AreaComponent, the area tags must be<br />

rendered after the beginning map tag and before the ending map tag. To accomplish this, the<br />

MapRenderer class renders the beginning map tag in encodeBegin and the rest of the map tag in<br />

encodeEnd.<br />

<strong>The</strong> <strong>Java</strong>Server Faces implementation automatically invokes the encodeEnd method of<br />

AreaComponent’s renderer after it invokes MapRenderer’s encodeBegin method and before it<br />

invokes MapRenderer’s encodeEnd method. If a component needs to perform the rendering for<br />

its children, it does this in the encodeChildren method.<br />

Here are the encodeBegin and encodeEnd methods of MapRenderer:<br />

public void encodeBegin(FacesContext context,<br />

UIComponent component) throws IOException {<br />

if ((context == null)|| (component == null)){<br />

throw new NullPointerException();<br />

}<br />

MapComponent map = (MapComponent) component;<br />

ResponseWriter writer = context.getResponseWriter();<br />

writer.startElement("map", map);<br />

writer.writeAttribute("name", map.getId(),"id");<br />

}<br />

public void encodeEnd(FacesContext context) throws IOException {<br />

if ((context == null) || (component == null)){<br />

throw new NullPointerException();<br />

}<br />

MapComponent map = (MapComponent) component;<br />

ResponseWriter writer = context.getResponseWriter();<br />

writer.startElement("input", map);<br />

writer.writeAttribute("type", "hidden", null);<br />

writer.writeAttribute("name",<br />

getName(context,map), "clientId");(<br />

writer.endElement("input");<br />

writer.endElement("map");<br />

}<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!