Custom converters :-
Java
Server Faces provides set of standard converters but in some cases we
need to implement our own converters.
We
want to be date in a particular format or  emailId should be of
specific format etc.
We can implement this by using java script but if java script is disabled? So its always recommended to perform server side validations.
We can implement this by using java script but if java script is disabled? So its always recommended to perform server side validations.
Steps
to be followed for creating custom converters :-
- Create a Java class
- Implement javax.faces.convert.Converter
- Override the unimplemented methods getAsObject(FacesContext,UIComponent,String),getAsString(FacesContext,UIComponent,String)
Example
:-
public
class
CustomConverter implements
Converter {
 @Override
 public
Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
   throws
ConverterException {
  //
TODO
Auto-generated method stub
  return
null;
 }
 @Override
 public
String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)
   throws
ConverterException {
  //
TODO
Auto-generated method stub
  return
null;
 }
}
As
we get data from web page in form of String all the conversion 
is
done in  getAsObject();
4.Registering custom converter class in faces-config.xml
<converter>
  <description>Removes
white spaces in the input</description>
  <display-name></display-name>
  <converter-id>cc</converter-id>
  <converter-class>CustomConverter</converter-class>
</converter>
This
can be done by component tab in faces-config.xml instead of writing
the above lines.
The
above converter  can be implemented by using
1.Converter
attribute :-
<h:inputText
id="number"
value="#{loginBean.num}"
converter="cc">
</h:inputText>
- Using Converter tag nested inside the component tag
<h:inputText id="number" value="#{loginBean.num}">
    <f:converter
id="cc"/>
</h:inputText>
Happy
Learning
Please
provide your valuable comments on this article and share it across
your network.
Contact
me @ sudheer@javarecent.com
 or admin@java-recent.com
 
 
No comments:
Post a Comment