Updated April 3, 2023
Introduction to JSF Converters
In JSF (JavaServer Faces) application, the user inputs are sent to the server using an http request as a client request. These user inputs are referred to as values. These request values are sent to the server in the form of a string. However, the JSF application uses various data types such as int, float, double, String, Boolean, date and so on. Therefore before the request values are processed need to be transformed into the appropriate data types. This transformation process is referred to as conversion. In this topic, we are going to learn about JSF Converters.
To accomplish conversion in the JSF application, the JSF framework provides standard converters. These converters are provided as the tag in the core tag library of JSF. In addition, you can create your own converters to accomplish the application requirements. These converters are referred to as custom converters.
Converter tags
In the JSF application, data entered by the users into the UI components need to be converted into an appropriate format before it can proceed by an application. The following table lists the standard tags provided by the JSF core tag library for performing conversions.
JSF core tags for data conversion –
f:converter – This tag is used to add an arbitrary converter to the instance parent component.
Example
<f:converter converterId = "javax.faces.Integer"/>
f:convertNumber – This tag is used to add a “NumberConverter” instance to the parent component.
Example
<c:convertNumber type = "javax.faces.Integer"/>
f:convertDateTime – This tag is used to add a “DateTimeConverter” instance to the parent component.
Example
<f:convertDateTime pattern = "dd/mm/yyyy"/>
The JSF framework provides standard converters for numbers and dates. Sometimes in an application, we need to convert the user input to number and date data types, so we can use the JSF framework to provide standard converters. All the standard converters contain in the javax.faces.convert package JSF framework. All the converters are implicitly applied based on the value of the component and if we want to access these converters, we can access these by converter Id.
JSF framework Converters
The IntegerConverter class which uses to convert user input string values into java.lang.Integer type of values and its converter id is javax.faces.Integer.
Example
<h:inputText id="age" converter="javax.faces.Integer" />
The BigIntegerConverter class which uses to convert user input string values into java.lang.BigInteger type of values and its converter id is javax.faces.BigInteger.
Example
<h:inputText id="age" converter="javax.faces.Integer" />
The same way can be used for the different integer types.
The ShortConverter class which uses to convert user input string values into java.lang.Short type of values and its conveter id is javax.faces.Short.
The LongConverter class which uses to convert user input string values into java.lang.Short type of values and its conveter id is javax.faces.Long.
The NumberConverter class which uses to convert user input string values into java.lang.Number type of values and its conveter id is javax.faces.Number.
Example
<h:outputText value="#{userBean.height}">
<f:convertNumber maxFractionDigits="2" />
</h:outputText>
The FloatConverter class which uses to convert user input string values into java.lang.Float type of values and its conveter id is javax.faces.Float.
The BigDecimalConverter class which uses to convert user input string values into java.lang. BigDecimal type of values and its conveter id is javax.faces.BigDecimal.
The DoubleConverter class which uses to convert user input string values into java.lang. Double type of values and its conveter id is javax.faces.Double.
The ByteConverter class which uses to convert user input string values into java.lang.Byte type of values and its conveter id is javax.faces.Byte.
The CharacterConverter class which uses to convert user input string values into java.lang. The character type of values and its conveter id is javax.faces.Character.
The BooleanConverter class which uses to convert user input string values into java.lang.Boolean type of values and its conveter id is javax.faces.Boolean.
The DateTimeConverter class which uses to convert user input string values into java.lang. DateTime type of values and its conveter id is javax.faces.Datetime.
The EnumConverter class which uses to convert user input string values into java.lang. Enum type of values and its conveter id is javax.faces.Enum.
1. convertDateTime Tag
The JSF convertDateTime contains the following attributes to convert the Date time format.
- dateStyle – This attribute specifies the formatting style for the date of a date string is to be formatted.
- locale – This attribute specifies to represent a date in Locale format.
- pattern – This attribute specifies the formatting pattern to be the format.
- timeStyle – This attribute specifies the Predefined formatting style for the date of a date string is to be formatted.
- timeZone – This attribute specifies the Time zone for the date of a date string is to be formatted.
- type – This attribute specifies the date or/and time or both to be formatted.
Example
<h:inputText id="DOB" label = "Date of Birth" value="#{bean.DOB }">
<f:convertDateTime pattern="dd/mm/yyyy" />
</h:inputText>
2. convertNumber Tag
The JSF convertNumber contains the following attributes to convert Number format. currencyCode – This attribute specifies to apply the currency format.
- currencySymbol – This attribute specifies to apply the currency format.
- groupingUsed – This attribute specifies whether the formatted output has grouping separators or not.
- integerOnly – This attribute specifies whether only an integer part format or not.
- locale – This attribute specifies to represent the number in Locale format.
- minFractionDigits – This attribute specifies the minimum number of digits in the fractional part.
- maxFractionDigits – This attribute specifies the maximum number of digits in the fractional part.
- minIntegerDigits – This attribute specifies the minimum number of digits in the integer part.
- maxIntegerDigits – This attribute specifies the maximum number of digits in the integer part.
- pattern – This attribute specifies the formatting pattern to be the format.
- type – This attribute specifies whether the type of number, percent and currency.
Example
<h:outputText value = "#{bean.height}">
<f:convertNumber maxFractionDigits = "1" />
</h:outputText>
Let’s see an example of the JSF project.
Create index.xhtml with the following code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:form>
<h:outputLabel for="name">Name : </h:outputLabel>
<h:inputText id="name" value="#{Emp.name}"/><br/>
<h:outputLabel for="eid">Eid : </h:outputLabel>
<h:inputText id="eid" value="#{Emp.eid}">
<h:outputLabel for="sal">Salary : </h:outputLabel>
<h:inputText id="sal" value="#{Emp.sal}">
<f:converter converterId="javax.faces.Integer" />
</h:inputText><br/>
<h:commandButton action="disp.xhtml" value="Submit Query"/>
</h:form>
</html>
Create Emp.java class with the following code in the project.
package jsfp;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class Emp {
String name;
String eid;
int sal;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEid() {
return eid;
}
public void setEid(String eid) {
this.eid = eid;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
}
Create disp.xhtml for the response with the following code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Reply Page</title>
</h:head>
<h:body>
<h:outputText value="Welcome #{Emp.name}. Your eid is #{Emp.eid}. Your Salary is #{Emp.sal}."/>
</h:body>
</html>
An output of the above project in the sequence is –
You fill the details as below –
Once you click the button the output is –
Conclusion
The user inputs are sent to the server using an http request in the form of the string, the request values to be processed first need to be transformed into the appropriate data types such as int, float, double, String, Boolean, date, and so on by using the JSF Converters of JSF framework.
Recommended Articles
This is a guide to JSF Converters. Here we discuss the Converter Tags and framework of JSF Converters along with the examples. You may also have a look at the following articles to learn more –