Converters in JSF 2

 Converters in JSF2 are used to easily convert a value from one data type to other. The following inbuilt converters are available in JSF2.

Converter Name Converter Class
Byte Converter javax.faces.convert.ByteConverter
Short Converter javax.faces.convert.ShortConverter
Character Converter javax.faces.convert.CharacterConverter
Boolean Converter javax.faces.convert.BooleanConverter
Integer Converter javax.faces.convert.IntegerConverter
Float Converter javax.faces.convert.FloatConverter
Double Converter javax.faces.convert.DoubleConverter
Long Converter javax.faces.convert.LongConverter
Big Integer Converter javax.faces.convert.BigIntegerConverter
Big Decimal Converter javax.faces.convert.BigDecimalConverter
Date Time Converter javax.faces.convert.DateTimeConverter
Number Converter javax.faces.convert.NumberConverter
Enum Converter javax.faces.convert.EnumConverter

 The JSF2 also allow developers to create a custom converter for their own purpose. The following rules are very important because almost all the developers are makes lot of mistake here.
 1. Before going to use any converter, we need to register that converter into faces-config.xml file (If you are going to create custom converter, this is important). Se the below example, Here I have registered Integer converter into facex-config.xml file.


 2. By default all converter have error message. If converter can't able to convert the value, then it will display error message. So we need to register error message component for corresponding converter component.
 3. We need to bind a value of the component to managed bean property as the same type as the converter.
 4. We have two ways for registering converter component. The first one is using converter attribute directly on the component tag and another one is, we can nest a converter tag within the component tag and use converter tag's converterId attribute or binding attribute to reference the converter. Examples are here.

 In my upcoming post, I am going to give a small example for above converters.


Popular Posts