|
JSF Application |
|
Custom Converter Example in JSF
JSF provides a very flexible architecture that
not only let the developers use converters
provided already by the implementation but also
create their own converters according to the
requirement of the application. This topic
explains about how to create custom converter.
When the user enters value to the component,
it’s simply a string value. Now you may be in
the need of using this value as a different object
like Boolean, Date etc. Converters can help in
this conversion. JSF framework has provided
many converters like Boolean Converter, Byte
Converter, Number Converter etc. These
converters convert values into appropriate type
of object and return it also to the page in the
appropriate format. JSF flexible architecture
provides you freedom to create your own
converters. These can be used to check the
value in the correct format. For example, In
our application user is provided an input box
to fill time in “hours:minutes:seconds” format.
This String is converted as Object by the
converter and also converted back in String
when it needs to display in the web page. Now
if the user doesn’t fill time in correct format
then it displays error message showing the
conversion could not be successful.
To create custom converter you need to
implement “Converter” interface of
javax.faces.converter package in your
class.
Steps to follow:
1. Create a class that implements
javax.faces.converter.Converter
interface.
2. Import necessary packages and classes.
3. Implement two abstract classes
“getAsObject()”, “getAsString()”
provided by Converter interface.
getAsObject() method converts the
String (User Input) to Object and
getAsString() method converts the
Object to String to send back to the
page.
4. Configure the configuration file (facesconfig.
xml) adding <converter>
element. This element has child elements |
|
<converter-id> (name of the converter
to be used while programming) and <converter-class> (name of the
converter class).
5. Create view page where <f:converter>
tag is used with attribute “converterId” which specifies the
name of the converter specified in <converter-id> element of <converter> element in “facesconfig.
xml” file.
6. Use <h:message> tag to display the
error message.
The steps above have been implemented in
our application “customconverter”. This will
help you to understand the process of
creating custom converter. Just go through
the following steps:
Step1: Create a class “TimeConverter” that
implements the Converter interface and
implements two abstract methods
“getAsObject()”, “getAsString()”. Save
this file as “TimeConverter.java” in WEBINF/
classes directory of your application in
Tomcat server.
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.*;
import javax.faces.application.*;
public class TimeConverter implements
Converter {
public TimeConverter() {
}
public Object getAsObject(FacesContext
facesContext, UIComponent uiComponent,
String param){
try {
String hr_mi_se[] = param.split(“:”);
int seconds =
Integer.parseInt(hr_mi_se[0])*60*60 +
Integer.parseInt(hr_mi_se[1])*60+
Integer.parseInt(hr_mi_se[2]);
Integer sObject= new Integer(seconds);
|
|
|
Dec
2007 | Java Jazz Up |62 |
|
|
View All Topics |
All Pages of this Issue |
Pages:
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53 ,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63 ,
64,
65 ,
66 ,
67 ,
68 ,
69 ,
70 ,
71 ,
72 ,
73 ,
74 ,
75 ,
76 ,
77 ,
78 ,
79 ,
80 ,
81 ,
82 , Download PDF |
|
|
|
|
|
|
|
|
|