×
☰ See All Chapters

JSF Custom Converter <f:converter> Tag

  1. Create a converter class by implementing javax.faces.convert.Converter interface. You should implement the getAsObject() and getAsString() methods. getAsObject() method is invoked during the APPLY REQUEST VALUES PHASE and getAsString() method is invoked during the RENDER RESPONSE PHASE. 

  2. Use Annotation @FacesConvertor to assign a unique name to the custom convertor. This unique name should be used in facelets to use this converter. 

jsf-custom-converter-0
 

JSF Custom Converter <f:converter> Tag Example

In the below example we have created custom converter for date to be in the format ####.00, for example 12.50, 11.00 are valid and 12.5, 11.000 are invalid.

jsf-custom-converter-1
 
jsf-custom-converter-2
 

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>JSF_CustomCoverter</artifactId>

        <packaging>war</packaging>

        <version>1.0-SNAPSHOT</version>

        <dependencies>

                <dependency>

                        <groupId>com.sun.faces</groupId>

                        <artifactId>jsf-api</artifactId>

                        <version>2.2.6</version>

                </dependency>

                <dependency>

                        <groupId>com.sun.faces</groupId>

                        <artifactId>jsf-impl</artifactId>

                        <version>2.2.6</version>

                </dependency>

 

                <dependency>

                        <groupId>javax.servlet</groupId>

                        <artifactId>servlet-api</artifactId>

                        <version>2.5</version>

                </dependency>

 

                <dependency>

                        <groupId>javax.servlet.jsp</groupId>

                        <artifactId>jsp-api</artifactId>

                        <version>2.1</version>

                </dependency>

 

        </dependencies>

        <build>

                <finalName>JSF_CustomCoverter</finalName>

                <plugins>

                        <plugin>

                                <groupId>org.apache.maven.plugins</groupId>

                                <artifactId>maven-compiler-plugin</artifactId>

                                <version>2.3.1</version>

                                <configuration>

                                        <source>1.9</source>

                                        <target>1.9</target>

                                </configuration>

                        </plugin>

                </plugins>

        </build>

 

</project>

web.xml

<!DOCTYPE web-app PUBLIC

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 "https://java.sun.com/dtd/web-app_2_3.dtd" >

 

<web-app>

        <display-name>Archetype Created Web Application</display-name>

        <servlet>

                <servlet-name>Faces Servlet</servlet-name>

                <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

                <load-on-startup>1</load-on-startup>

        </servlet>

        <servlet-mapping>

                <servlet-name>Faces Servlet</servlet-name>

                <url-pattern>*.xhtml</url-pattern>

        </servlet-mapping>

</web-app>

HelloBean.java

package com.java4coding;

 

import javax.faces.bean.ManagedBean;

 

@ManagedBean

public class HelloBean {

        SalaryTO salaryTO = new SalaryTO();

 

        public SalaryTO getSalaryTO() {

                return salaryTO;

        }

 

        public void setSalaryTO(SalaryTO salaryTO) {

                this.salaryTO = salaryTO;

        }

 

        public String display() {

                return "second";

        }

}

SalaryTO.java

package com.java4coding;

 

public class SalaryTO {

        private double salary;

 

        public double getSalary() {

                return salary;

        }

 

        public void setSalary(double salary) {

                this.salary = salary;

        }

}

SalaryConverter.java

package com.java4coding;

 

import javax.faces.component.UIComponent;

import javax.faces.context.FacesContext;

import javax.faces.convert.Converter;

import javax.faces.convert.ConverterException;

import javax.faces.convert.FacesConverter;

 

@FacesConverter(value = "salconverter")

public class SalaryConverter implements Converter {

 

        // this method is invoked during the APPLY REQUEST VALUES PHASE

        // this is used to accept value in the format "####.00"

        public Object getAsObject(FacesContext context, UIComponent component, String value) {

                System.out.println("Inside getAsObject() +++++++++++++++++");

                String s = value;

                if (!(s.matches("(?<![-.])\\b[0-9]+\\b(?!\\.[0-9])"))) {

                        SalaryTO salaryTO = new SalaryTO();

                        salaryTO.setSalary(Double.parseDouble(value));

                        return salaryTO;

                } else

                        throw new ConverterException();

        }

 

        // this method is invoked during the RENDER RESPONSE PHASE

        // this is used to print salary in the format "###.00 Rupees"

        public String getAsString(FacesContext context, UIComponent component, Object value) {

                System.out.println("Inside getAsString() +++++++++++++++++");

                SalaryTO salaryTO = (SalaryTO) value;

                Double sal = salaryTO.getSalary();

                String s = new String(sal + "0" + "  " + "Rupees");

                return s;

        }

}

first.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="https://www.w3.org/1999/xhtml"

        xmlns:h="https://xmlns.jcp.org/jsf/html"

        xmlns:f="https://xmlns.jcp.org/jsf/core">

 

<h:head></h:head>

<h:body style="background-color:yellow">

        <h:form>

                <h:panelGrid columns="1">

                        <h1>Please enter your Salary</h1>

 

                        <h:inputText id="sal" value="#{helloBean.salaryTO}"

                                converterMessage="Please enter salary in the farmat ####.00">

                                <f:converter converterId="salconverter" />

                        </h:inputText>

                        <h:message for="sal" style="color:red" />

                        <h:commandButton value="SUBMIT" action="#{helloBean.display}" />

                </h:panelGrid>

        </h:form>

</h:body>

</html>

second.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="https://www.w3.org/1999/xhtml"

        xmlns:h="https://xmlns.jcp.org/jsf/html"

        xmlns:f="https://xmlns.jcp.org/jsf/core">

<h:body style="background-color:wheat">

        Your Salary is

        <h:outputText value="#{helloBean.salaryTO}">

                <f:converter converterId="salconverter" />

        </h:outputText>

 

</h:body>

</html>

Eclipse project directory structure

jsf-custom-converter-3
 

Output

 

 

jsf-custom-converter-4
 
jsf-custom-converter-5
 

All Chapters
Author