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.
ADVERTISEMENT Popular Course in this category JSF Java Server Faces - Learning Path | 6 Course SeriesStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
The above is the detailed content of JSF Converters. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1379
52
Perfect Number in Java
Aug 30, 2024 pm 04:28 PM
Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.
Random Number Generator in Java
Aug 30, 2024 pm 04:27 PM
Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.
Weka in Java
Aug 30, 2024 pm 04:28 PM
Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.
Smith Number in Java
Aug 30, 2024 pm 04:28 PM
Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.
Java Spring Interview Questions
Aug 30, 2024 pm 04:29 PM
In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.
Break or return from Java 8 stream forEach?
Feb 07, 2025 pm 12:09 PM
Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
TimeStamp to Date in Java
Aug 30, 2024 pm 04:28 PM
Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.
Java Program to Find the Volume of Capsule
Feb 07, 2025 am 11:37 AM
Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4


