Sunday, September 26, 2010

Custom Converter Implementation.




Need for Custom Converter:

Let’s say we have an unique requirement of changing the inputted string to its Uppercase type. So we don’t have any specific Standard Converter who accomplishes the above objective. In that case we need to create a new class which handles the above requirement.


Implementation of Custom Converters:

JSF Page:




<f:subview id="converter3">
<h:form>
<br/>
<br/>
<h:outputLabel value="UpperCase Converter :"/>
<h:inputText id="text1" converter="uppercaseConverter"
converterMessage="Not a String"
value="#{startPage.converterChangeCase}"
onchange="submit()"/>
<h:message for="text1"/>
</h:form>
</f:subview>



Explanation:

Here the <h:inputText> tag has an attribute converter which is mapped in the faces-config.xml which finally points to the provider class i.e UppercaseConverter.java in our case.


Faces-config.xml:




<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee">
<managed-bean>
<managed-bean-name>startPage</managed-bean-name>
<managed-bean-class>com.java.jsf.view.backing.StartPage</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<!--oracle-jdev-comment:managed-bean-jsp-link:1WEB-INF/pages/start_page.jspx-->
</managed-bean>
<converter>
<converter-id>uppercaseConverter</converter-id>
<converter-class> com.java.jsf.view.util.UpperCaseConverter
</converter-class>
</converter>
</faces-config>



Explanation:

Well, a custom converter before being used has to be specified inside the faces-config.xml. This is done by using <converter> paired tag which has <converter-id> and <converter-class> as child tags in it which specifies the address of the converter class.

Converter Class:




public class UpperCaseConverter implements Converter{
public Object getAsObject(FacesContext facesContext,UIComponent component,String stringValue){
return stringValue.toUpperCase();
}

public String getAsString(FacesContext facesContext,
UIComponent uiComponent, Object objectValue) {
return objectValue.toString();
}
}



Explanation:

The custom converter class should implement Converter interface. Another important factor that you have to take note of is that, it should implement two abstract methods in the class Converter. They are as follows-
getAsObject() [Converts from String user input to Object]
getAsString() [Converts from Object to String & back to screen]


Managed Bean :




public class StartPage{
private String converterChangeCase;

public void setConverterChangeCase(String converterChangeCase) {
System.out.println("Change Case : "+converterChangeCase);
this.converterChangeCase = converterChangeCase;
}

public String getConverterChangeCase(){
return converterChangeCase;
}
}




Explanation:

The managed bean has got an attribute converterChangeCase along with its accessor methods which is finally receives the input from the JSF component. This can be verified from the sys-out statement present inside the setter method.

Hope this will help you in creating your own customized CONVERTERS.
Good Night !!!

No comments:

Post a Comment