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:
1.
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
<converter>
<converter-id>CConverter</converter-id>
<converter-class>CustomConverter</converter-class>
</converter>

2 Use Annotations:
@FacesConverter(value=”CConverter”)

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 – creditnumber.java

@ManagedBean
public class creditnumber {
    
    String cnum;

    public String getCnum() {
        return cnum;
    }

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

CustomConverter.java

public class CustomConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String nvalue) 
    {
        String Cnumber=nvalue.trim();
        int length=Cnumber.length();
        
        if(length!=16)
        {
            FacesMessage message=new FacesMessage();
            message.setSummary("Invalid Number - Must have 16 digits");
            throw new ConverterException(message);
        }
        else
        {
            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();
            sb.append(part1+"-");
            sb.append(part2+"-");
            sb.append(part4+"-");
            sb.append(part4);
            return sb.toString();   
        } 
    }

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

 

Custom.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://java.sun.com/jsf/core">
    <h:head>
        <title>Enter Credit Card Number</title>
    </h:head>
    <h:body>
        <h:form>
            <h:outputLabel value="Enter credit card number"/>
            <h:inputText value="#{creditnumber.cnum}">
                <c:converter converterId="CConverter"/>
            </h:inputText>
            <br></br>
           <h:commandButton value="Convert" action="show"/>  
        </h:form>
    </h:body>
</html>

show.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:outputText value="#{creditnumber.cnum}"/>
    </h:body>
</html>

Output:
out1
If entered Correctly,
out2

 

 

 

 

 

 

 

 

By Sri

Leave a Reply

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