Scenario :Suppose you have a form which has plenty of attribute and also they are difference data type .example :name ,username , password ,age ,date etc. when your enter the value in this field they are in form of String .but in model they have different type of Data .The question is that how to solve this problem .
Converter : Converter is used to convert one type of object to difference type of object. For the Ui point of view only String datetype /object is used .but if you want to save form value in difference data type then you should need to take the help of converter .
Referential Definition : conversion is the process of ensuring data is of the right object or type
Converter is broadly divided in two type
1-Standard Converters
2-Customized Converter
Q-Which Phase is responsible for calling the converter ?
Answer: before calling the update model value phases the converter should be called .so apply request value phase is responsible for triggering the converter .
Standard Converters :is type of converters which are provided by jsf it self . standard converters is used for most of the common conversion process.
Example of the standard converter and converter id
Converter id converter class
1-javax.faces.BigDecimal javax.faces.convert.BigDecimalConverter
2-javax.faces.BigInteger javax.faces.convert.BigIntegerConverter
3-javax.faces.Boolean javax.faces.convert.BooleanConverter
4-javax.faces.Byte javax.faces.convert.ByteConverter
5-javax.faces.Character javax.faces.convert.CharacterConverter
6-javax.faces.DateTime javax.faces.convert.DateTimeConverter
7-javax.faces.Double javax.faces.convert.DoubleConverter
8-javax.faces.Float javax.faces.convert.FloatConverter
When you call any Standard Converters you should call using it converter id not it class .
JSF have three type of the predefine tag for converter .these are following
1-<f:converter converterId="javax.faces.Short" binding="" />
This tag is used to called the Standard Converters .if you want to convert any input text value in any existing Standard data type .then you need to use this tag .
2- <f:convertNumber maxFractionDigits="2" groupingUsed="true" currencySymbol="$" maxIntegerDigits="7" type="currency"/ >
This is used to convert the String to number type .it’s also have type attribute which is following three type
1- currency
2- number
3- percent
3- <f:convertDateTime pattern="MM/yyyy"/> "/ >
This tag is used to convert the string into specify type of the pattern
This is enough theory let go for the practical approach .
For show these tag I need to write a simple jsp page which have three different type of properties for holding data .
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>User Registration.jsp Form</title>
</head>
<body>
<f:view>
<h2>User Form </h2>
<h:form id="userForm">
<h:outputLabel for="age" id="age">
<h:outputText value="Age"/>
</h:outputLabel>
<h:inputText id="age" value="#{user.age}">
<f:converter converterId="javax.faces.Short" binding="" />
</h:inputText>
<h:message for="age"/>
<h:outputLabel for="salary" id="salary">
<h:outputText value="Salary"/>
</h:outputLabel>
<h:inputText id="salary" value="#{user.salary}" >
<f:convertNumber maxFractionDigits="5" groupingUsed="true" currencySymbol="@" maxIntegerDigits="4" type="currency"/ >
</h:inputText>
<h:message for="salary"/>
<h:outputLabel for="birthDate" id="birthdate">
<h:outputText value="Birth Date"/>
</h:outputLabel>
<h:inputText id="birthDate" value="#{user.birthDate}" >
<f:convertDateTime pattern="MM/yyyy"/>
</h:inputText>
<h:message for="birthDate"/>
<h:commandButton id="submit" value="Submit" />
</h:form>
</f:view>
</body>
</html>
Bean Class Code: User.java
import java.io.Serializable;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
public class User implements Serializable {
private short age;
private Date birthDate;
private float salary;
/**
* @return Returns the age.
*/
public short getAge() {
return age;
}
/**
* @param age The age to set.
*/
public void setAge(short age) {
this.age = age;
}
public Date getBirthDate() {
return birthDate;
}
/**
* @param birthDate The birthDate to set.
*/
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
/**
* @return Returns the salary.
*/
public float getSalary() {
return salary;
}
/**
* @param salary The salary to set.
*/
public void setSalary(float salary) {
this.salary = salary;
}
public void register(ActionEvent event) {
/**
* Application logic
*/
}
}
custom converters :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 method 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-then used this converter in your application using
tag
when apply restore view phases is called 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 <f:converter/> tag to call this conveter .
<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<f:converter/> .because it automatically figur out that this class is bind with this Converter like this
<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
<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>