JSF Tip #47 - Package a custom converter

Just like a composite component you might want to distribute a converter, but how would you package it? Easy, read the rest down below!

The 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 {

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

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

The faces-config.xml


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

    <faces-config version="2.1"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
            http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd">
    </faces-config>
        

Posted November 26, 2013

Up