1) Introduction
Java Server Faces provides a component-based event-driven model for User Interface Layer in a Web Application. JSF UI Components behave very similar to Swing Components in which case Events are made to trigger by the User and they are handled by appropriate Listeners. In this article, we will focus on Converters which remains an important aspect in converting the user input to the one that is stored in the Mode. This article won't provide the introductory details related to JSF and novice users are advised to have to a glance over this Introduction to Java Server Faces article on JavaBeat. This article will begin with explaining the need to have Converters in JSF Framework and then continue to discuss about the various Standard Converters that are available in-built with JSF. Then the final section will show you how to develop and register Custom Converters to a JSF Application.
2) Converters in JSF
Before getting into details, let us study the need and advantage of having JSF Converters in place. Let us take an example of a Web Application in which the User will be presented with a Web Page for entering the input values. The values entered by the User in all cases will be in string format. Even, when asked to enter numerical values like age or salary whatever is being passed on to the Server end will still be in String format. So, there must be some intermediary which will do the job of converting the string values into the type that is appropriate for the particular field value.
Say, for example, if the User Form contains fields for name, age, salary and date of birth. Obviously, the model object for this form will contain string, integer, double and date data-types for storing name, age, salary and date of birth respectively. So the Converters do the job of converting the user input. JSF already comes with a bunch of Standard Converters (which will be covered shortly) for most of the common conversion process. If you can't find a suitable converter that is very specific to your Application, then still you can define a Customized Converter that will do the job for you.
In techincal terms, the Apply Request Value Phase is the phase during which the conversion of values to their appropriate type will happen. 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 in which case the model values that are associated for each UI Component would not be updated. It gives a chance to the user to re-enter the values. If the user enters the right value, and if the Conversion and the validation succeeds, then the Model values are updated.
|