TOC PREV NEXT INDEX




Part I. Creating a Direct-to-DOM Renderer for a Standard UIInput Component


This part of the tutorial will guide you through the process of creating a Direct-to-DOM renderer for a standard component. The source code for this tutorial is included in the basicInputText sample tutorial application.

The renderer is responsible for processing the form data for the component. This is the decoding of the component which is done in the decode method of the renderer. The renderer is also responsible for translating a component into a W3C DOM Element. This is the encoding of the component which is done in the encode methods of the renderer. With a standard component, such as a UIInput with no children, you need only implement the encodeEnd method. The encodeBegin and encodeChildren methods do not need to be implemented for this renderer.

The first method to look at is decode(FacesContext, UIComponent). This method is responsible for taking any parameters that were passed in from a form post and setting the value on the component. The first thing the method does is to validate the context and component parameters, checking to ensure that they are not null. Next, the method ignores all but UIInput components. If the component is a UIInput, then any new value is extracted from the request and put on the component as the submittedValue.

public void decode(FacesContext facesContext, UIComponent uiComponent) {
 
validateParameters(facesContext, uiComponent, null);
 
// only need to decode input components
 
if (!(uiComponent instanceof UIInput)) {
 
return;
 
}
 
// only need to decode enabled, writable components
 
if (isStatic(uiComponent)) {
 
return;
 
}
 
// extract component value from the request map
 
String clientId = uiComponent.getClientId(facesContext);
 
if (clientId == null) {
 
System.out.println("Client id is not defined for decoding");
 
}
 
Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
 
if (requestMap.containsKey(clientId)) {
 
String decodedValue = (String) requestMap.get(clientId);
 
// setSubmittedValue is a method in the superclass DomBasicInputRenderer
 
setSubmittedValue(uiComponent, decodedValue);
 
}
 
} // end decode
 

The next method to analyze is the encodeEnd(FacesContext, UIComponent). This method is responsible for building the DOM Element to represent the component in the browser. The encodeEnd method uses the DOM methods exposed in the ICEfaces DOMContext and the W3C DOM API. The DOMContext.attachDOMContext method is used to provide a DOMContext to the encodeEnd method. The DOMContext will be initialized if required. As part of the initialization, a root node is created using the DOMContext createRootElement method. The DOMContext API will be used to create new Elements and TextNodes. To set Element attributes and append child nodes to Elements, the W3C DOM API will be used.


 
public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
 
		throws IOException {
 
DOMContext domContext = DOMContext.attachDOMContext(facesContext, uiComponent);
 
if (!domContext.isInitialized()) {
 
Element root = domContext.createRootElement("input");
 
setRootElementId(facesContext, root, uiComponent);
 
root.setAttribute("type", "text");
 
root.setAttribute("name", uiComponent.getClientId(facesContext));
 
}
 

 
Element root = (Element) domContext.getRootNode() ;
 
root.setAttribute("onkeydown",this.ICESUBMIT) ;
 

 
// create a new String array to hold attributes we will exclude
 
// for the PassThruAttributeRenderer
 
String[] excludesArray = new String[1] ;
 
excludesArray[0] = "onkeydown" ;
 

 
// the renderAttributes method will not overwrite any attributes
 
// contained in the excludesArray
 
PassThruAttributeRenderer.renderAttributes(facesContext, uiComponent, 
excludesArray); 
 
} // end encodeEnd
 

The following is the content for the faces-config.xml file used in this tutorial. The faces config is used to configure the Renderer for the UIInput Text Component as well as any managed-beans that are required.

<?xml version="1.0"?>
 
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces 
Config 1.0//EN" "http://java.sun.com/dtd/web-facesconfig_1_0.dtd" >
 
<faces-config>
 
<render-kit> description>Tutorial Basic Renderer</description>
 
<renderer>
 
<component-family>javax.faces.Input</component-family>
 
<renderer-type>javax.faces.Text</renderer-type>
 
<renderer-class>
 
com.icesoft.tutorial.TutorialInputTextRenderer
 
</renderer-class>
 
</renderer>
 
</render-kit>
 
<managed-bean>
 
<managed-bean-name>tutorial</managed-bean-name>
 
<managed-bean-class>com.icesoft.tutorial.TutorialBean</managed-bean-class>
 
<managed-bean-scope>session</managed-bean-scope>
 
</managed-bean>
 
</faces-config>
 

 


Copyright 2005. ICEsoft Technologies, Inc.
http://www.icesoft.com

TOC PREV NEXT INDEX