Skip to main content

JSF Tip #46 - Creating a custom converter

Posted by mriem on November 25, 2013 at 3:55 AM PST

Note this blog is obsolete, see for the current blog

Sometimes you need to write a custom converter to converter the client side representation to an object instance and vice versa. What is need to use a custom converter? See below.

A custom converter

package customconverter;

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

@FacesConverter(value = "customConverterId")
public class CustomConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return new Custom();

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

And the page using it.

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE html>

<html xmlns=""
        <title>Custom Converter - f:converter tag</title>
        <h2>Custom Converter - f:converter tag</h2>
            Below you will see a string that was generated using a custom converter
            named 'customConverterId'. See the sources for the source for this
        <h:outputText value="#{customConverterBean.custom}">
            <f:converter converterId="customConverterId"/>

Note the code for this sample is available as part of the Glassfish samples (see the subversion repository at

And that is it.