This type of converter is base on the requirment of the application .if you want to convert any object in the basic of the application object then you should need to implement this way .you should follow these following step
1-fist create class which should be implement the Converter interface .
Interface name:Converter
package name :javax.faces.converter
method description :
this class have two method
1-getAsObject()
2-getAsString()
2-after first step implenment getAsobject .this method is responsible to convert the string(user in put value ) into any type of object(Application specific) .
3-implement the getAsString method which converts an object (application specific ) to string
4-then register this converter to faces-cofig.xml
5-the used this converter in your application using
when apply restore view execute then it called your implemented method getAsObject() .and convert user(your) input value which is string into the specified object .
and at last before render response phases it called the getAsStrign() method and convert specific type object into the string object .
Let Start with playing Code:
Step1:Create a class and Implement the Converter interface
public class ConvertClassWhichImplement implements Converter{
.......
..........
}
Step 2: Implement the getAsObject method
In this method i just create object of my bean class and assign the user value in this class
public Object getAsObject(FacesContext faces,UIComponent ui,String value){
UserClass claseuser=new UserClass();
claseuser.setUserName(value);
System.out.println("ConvertClassWhichImplement"+value);
return claseuser;
}
Step 3: Implement the getAsString method:
in this method i just convert the application object to string .
public String getAsString(FacesContext facescontext,UIComponent uicompnent,Object value){
return value.toString();
}
Step 4: Register custom converter with faces context:
This is a very importance step and we have two choice to impement this step
1-register our converter class with id like this.and used
<converter>
<converter-id>convertbyuser</converter-id>
<converter-class>ConvertClassWhichImplement</converter-class>
</converter>
2-second way going to minimize your work littel bits .in this way you just need to bind convert to class .and also you do not need to used the
<converter>
<converter-for-class>UserClass</converter-for-class>
<converter-class>ConvertClassWhichImplement</converter-class>
</converter>
Step 5: Use the converter tag in your JSP
yes friends this is last step .which show our whole work.This is for the first part of the fourth phases
1-<h:inputText value="#{userName.userclassvalue}" id="user1" >
<f:converter converterId="convertbyuser"/>
</h:inputText>
2-This is for second
</h:outputLabel>
<h:inputText value="#{userName.userclassvalue}" id="user1" >
</h:inputText>
No comments:
Post a Comment