The Encoding Methods
The Encoding Methods
Notice that encodeBegin renders only the beginning map tag. The encodeEnd method renders
the input tag and the ending map tag.
The encoding methods accept a UIComponent argument and a FacesContext argument. The
FacesContext
instance contains all the information associated with the current request. The
UIComponent
argument is the component that needs to be rendered.
The rest of the method renders the markup to the ResponseWriter instance, which writes out
the markup to the current response. This basically involves passing the HTML tag names and
attribute names to the ResponseWriter instance as strings, retrieving the values of the
component attributes, and passing these values to the ResponseWriter instance.
The startElement method takes a String (the name of the tag) and the component to which
the tag corresponds (in this case, map). (Passing this information to the ResponseWriter
instance helps design-time tools know which portions of the generated markup are related to
which components.)
After calling startElement, you can call writeAttribute to render the tag's attributes. The
writeAttribute
method takes the name of the attribute, its value, and the name of a property
or attribute of the containing component corresponding to the attribute. The last parameter can
be null, and it won't be rendered.
The name attribute value of the map tag is retrieved using the getId method of UIComponent,
which returns the component's unique identifier. The name attribute value of the input tag is
retrieved using the getName(FacesContext, UIComponent) method of MapRenderer.
If you want your component to perform its own rendering but delegate to a renderer if there is
one, include the following lines in the encoding method to check whether there is a renderer
associated with this component.
if (getRendererType() != null) {
super.encodeEnd(context);
return;
}
If there is a renderer available, this method invokes the superclass's encodeEnd method, which
does the work of finding the renderer. The MapComponent class delegates all rendering to
MapRenderer
, so it does not need to check for available renderers.
In some custom component classes that extend standard components, you might need to
implement other methods in addition to encodeEnd. For example, if you need to retrieve the
component's value from the request parameters, you must also implement the decode method.
Creating Custom Component Classes
Chapter 13 · Creating Custom UI Components
423