JSF Custom Converter

In Jsf we can create our own converter by implementing Converter interface. And we need to override two of its methods: getAsObject and getAsString.

Example:

index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:body>

<h3>JSF customConverter example</h3>

<h:form>
    Enter Name: 
<h:inputText id="name" value="#{user.name}" size="20"
required="true" >
<f:converter converterId="com.javasafari.CustomConverter" />
</h:inputText>

 
<h:commandButton value="Submit" action="output" />
 
</h:form>

</h:body>
</html>

User Bean:

package com.javasafari;


import java.io.Serializable;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="user")
@SessionScoped
public class User implements Serializable{
 
/**
* 
*/
private static final long serialVersionUID = 1L;
String name;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
  
}

CustomConverter class:


package com.javasafari;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter("com.javasafari.CustomConverter")
public class CustomConverter implements Converter {

// It is getting called when we are fetching value from page to bean for processing
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {

System.out.println("Input value : " + value);
return value + "as converted Value";
}

//It is getting called when we are goind to render value on page
public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
String output = (String) value;
System.out.println("Getting As String : " + value);
return output;
}
}

Output Page:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      >
    <h:body>
 
    <h3>JSF convertDate Example</h3>
 
ConvertedName :  
<h:outputText value="#{user.name}" >
<f:converter converterId="com.javasafari.CustomConverter" />
</h:outputText>
 
    </h:body>
</html>

Run Application:

jsf-custom-converter-tags

Enter Ankit and submit

jsf-custom-converter-tags

Instead of annotation we can do customConverter configuration in faces-config.xhtml also as:


<converter>
        <description>Custom Converter</description>
        <converter-id>customConverter</converter-id>
        <converter-class>
        com.javasafari.CustomConverter
        </converter-class>
    </converter>