JSF Converter Tutorial

Introduction

In JSF user input is normally of String type which will be submitted to the server for further processing.However when request parameters are mapped to different backing bean properties internally, the String values need to be converted to different data types, such as Integer,boolean,Short and Double.At the same time when the response is created for the user these java data types again need to be changed to String.

To deal with this scenario JSF Converters are used.JSF Converters will take care of converting the user input. JSF has bunch of Standard Converters for most of the conversion process. If you couldn't find a suitable converter specific to your requirement , then you can easily create a Custom Converter specific to your requirement.


Introduction

In JSF user input is normally of String type which will be submitted to the server for further processing.However when request parameters are mapped to different backing bean properties internally, the String values need to be converted to different data types, such as Integer,boolean,Short and Double.At the same time when the response is created for the user these java data types again need to be changed to String.

To deal with this scenario JSF Converters are used.JSF Converters will take care of converting the user input. JSF has bunch of Standard Converters for most of the conversion process. If you couldn't find a suitable converter specific to your requirement , then you can easily create a Custom Converter specific to your requirement.





Why Converters are important ?

Suppose in a Web Application user asked to provide some data. The values entered by the user is in string format. Even, numerical values like age, salary, phone no etc is also in String format. In this case when form will be submitted the values passed to Server will be in String format. So, there must be some mechanism which will convert the string values into the appropriate type as field value.

In the JSF life cycle, conversion occurs in the apply request values phase.If the conversion process succeeds, then the submitted values are validated against the Validators defined. If either Conversion or the Validation fails, then the form is re-displayed and the model values that are associated for each UI Component would not be updated. If user enters correct data, and Conversion and validation succeeds, then the Model values will be updated.


JSF Supplied Standard Converters

Let us discuss some standard Converters that comes bundled with JSF Implementation.JSF provides standard converters for all basic types like: Byte, Short, Integer,Long, Float, Double, Boolean, Character etc.

Note:-If you don't specify the converter for the component then JSF will apply the appropriate converter for the component. For example, if the component is associated with a property of type double, JSF will choose the Double converter.

Following are the standard Converters available in JSF.

  • DoubleConverter:– Used to convert the user input string values into values of type java.lang.Double
  • FloatConverter:– Used to convert the user input string values into values of type java.lang.Float
  • LongConverter:– Used to convert the user input string values into values of type java.lang.Long
  • IntegerConverter:– Used to convert the user input string values into values of type java.lang.Integer
  • NumberConverter:– Used to convert the user input string values into values of type java.lang.Number
  • ShortConverter:– Used to convert the user input string values into values of type java.lang.Short
  • DateTimeConverter:– Used to convert the user input string values into values of type java.lang.Date
  • BooleanConverter:– Used to convert the user input string values into values of type java.lang.Boolean



How to use standard converters


DateTime converter :

For all basic Java types JSF will automatically use converters. But if you want to format a Date object JSF provides a converter tag <f:convertDateTime>.This tag must be nested inside a component tag that supports converters.

For formatting date and time, we have to set the type as date. Whether the style of the date to displayed (short, medium, long) is specified in the dateStyle attribute.

<h:inputText id="date" value="#{sampleBean.date}"
           required="true">
    <f:convertDateTime type="date" dateStyle="long"/>
</h:inputText>

The converter DateTime supports attributes dateStyle, to configure the converter. The listing below shows the attributes you can use with the DateTime converter.


Attribute name

Description


dateStyle

Specifies the formatting style for the date
portion of the string. Valid options are short, medium
(default), long and full. Only valid if attribute
type is set.

timeStyle

Specifies the formatting style for the time
portion of the string. Valid options are short, medium
(default), long and full. Only valid if attribute
type is set.

timeZone

Specifed the time zone for the date. If not
set, Greenwich mean time (GMT) will be used.

locale

The specifed local to use for displaying this
date. Overrides the user’s current locale

pattern

The date format pattern used to convert this
number. Use this or the property type.

type

Specifies whether to display the date, time or
both.


Number converter

If you want to convert a component value to a Number type then <f:convertnumber/> tag can be used inside the component tag.It has many attribute like integerOnly, maxIntegerDigits, currencyCode,currencySymbol, pattern etc.

The following code defines a Number Converter which formats and displays the number in currency format. Depending on the locale set for this Application, it displays the currency symbol along with value. For example, it will display $1999, if the locale is pointing to US.

<h:inputtext id="salary" value="#{employeeBean.salary}" required="true">
<f:convertnumber type="currency"/>
</h:inputtext>

By specifying the type as 'percentage', the display value will have a '%' symbol followed by the original value.

<h:inputtext id="discount" value="#{testBean.discount}" required="true">
<f:convertnumber type="percent"/>
</h:inputtext>

We will discuss complete example for standard converter in separate article.


Custom Converter

In most of the scenario, JSF provided converters are sufficient. However, for application specific purposes, there may be a need to customize and convert the user entered string into some other data-type like phone no format. This can be done by using JSF Custom Converter.

Following steps are required to create Custom Converters in JSF.


Steps

  1. Create a converter class by implementing javax.faces.convert.Converter interface.
  2. Override both getAsObject() and getAsString() methods.
  3. Map the converter in faces-config.xml file
  4. Link your custom converter class to JSF component via f:converter tag.

We will discuss more about Custom converters with eclipse project in separate article.




comments powered by Disqus