Custom Converters in JSF
JSF has Standart Converters for numbers and dates. In addition to that you can also create custom converts in JSF.(you can find Standard Converters here)

Creating Custom Converters:
Create a java class implementing Converter Interface and provide implementation of these 2 methods

  • Object getAsObject(FacesContext context, UIComponent component, String value)
  • String getAsString(FacesContext context, UIComponent component, Object value)

2. Register the Custom Converter class
3. Associate Custom Converter with the UI

Implementing 2 Methods of Converter Interface:
Object getAsObject(FacesContext context, UIComponent component, String value)

  • The faces context represents the context of UI component which conversion is to be applied
  • UI component refers to the component to which converter to be added
  • String refers to the User input

String getAsString(FacesContext context, UI Component component, Object value)

Accepts the instance of FacesContext, UI Component and Object returned from getAsObject method.
This method converts the object to string to be displayed on a view

Registering Custom Converter class:
There are 2 ways
1. Register in faces.config.xml

2 Use Annotations:

Lets us consider, a customer enters his credit-card number.
Lets convert the credit card number in the format of 1111-2222-3333-4444.
Suppose if he enters the number less than 16 digits a message to be displayed.

JSF Managed Bean –

public class creditnumber {
    String cnum;

    public String getCnum() {
        return cnum;

    public void setCnum(String cnum) {
        this.cnum = cnum;
    public creditnumber() {

public class CustomConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String nvalue) 
        String Cnumber=nvalue.trim();
        int length=Cnumber.length();
            FacesMessage message=new FacesMessage();
            message.setSummary("Invalid Number - Must have 16 digits");
            throw new ConverterException(message);
            String part1=Cnumber.substring(0, 4);
            String part2=Cnumber.substring(4, 8);
            String part3=Cnumber.substring(8, 12);
            String part4=Cnumber.substring(12,16);
            StringBuilder sb=new StringBuilder();
            return sb.toString();   

    public String getAsString(FacesContext context, UIComponent component, Object value) 
        return value.toString();



<html xmlns=""
        <title>Enter Credit Card Number</title>
            <h:outputLabel value="Enter credit card number"/>
            <h:inputText value="#{creditnumber.cnum}">
                <c:converter converterId="CConverter"/>
           <h:commandButton value="Convert" action="show"/>  


<html xmlns=""
        <title>Facelet Title</title>
        <h:outputText value="#{creditnumber.cnum}"/>

If entered Correctly,









By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *