Monday, May 14, 2007

JSF component development

JSF components typically subclass UIComponentBase or one of it's subclasses depending on the functionality they provide.

Some of the properties and methods they must implement are:

getClientId() - returns the clientId

This is the type attribute through which we refer to the component in faces-config.xml
public static final String COMPONENT_TYPE = "";

This is the component family attribute used to associate renderers with the component.

public static final String COMPONENT_FAMILY="";

The purpose of this property is to help renderers determine if they can handle a particular component. Now if we implement the rendering in encodeBegin(), then will this property still be used?


The encodeBegin() method contains all the logic for rendering the component.

public void encodeBegin(FacesContext context) throws IOException

The encodeBegin() method must render output only is the method isRendered() returns true. This attribute tells us if the component must be rendered (something like isVisible).

The encoding process starts with a call to encodeBegin(). After it complete's, encodechildren() is called if the method getRendersChildren() returns true. By default it returns false.

public void encodeChildren(FacesContext context) throws IOException

After this the encodeEnd() method is called. It is important to implement this method only if the component has children. Otherwise th entire rendering logic can be put in encodeBegin().

public void encodeEnd(FacesContext context) throws IOException

If you want to delegate rendering to a renderer, then call the renderer instead of implementing the display logic in the encodeXXX() methods. UIComponent base invokes the default renderer. This can be achieved in a custom component by not overriding encodeBegin(). Renderers are usually needed if we want to support output in multiple markup languages.

The decode methd is used to interpret client input.

public void decode(FacesContext context)

The decode method should proceed only if the component is not disabled. Check by calling the isDisabled() method.

UIComponentBase also has methods for event handling. However we can rely on the superclass' implementation if we are not introducing any new events. [TODO: Add the methods...]

No comments: