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
<converter></converter>
f:convertNumber – This tag is used to add a “NumberConverter” instance to the parent component.
Example
<convertnumber></convertnumber>
f:convertDateTime – This tag is used to add a “DateTimeConverter” instance to the parent component.
Example
<convertdatetime></convertdatetime>
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
<inputtext converter="javax.faces.Integer"></inputtext>
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
<inputtext converter="javax.faces.Integer"></inputtext>
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
<convertnumber></convertnumber>
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
<convertdatetime></convertdatetime>
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
<convertnumber></convertnumber>
Let’s see an example of the JSF project.
Create index.xhtml with the following code.
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
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.
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <head> <title>Reply Page</title> </head> <body> <outputtext your salary is></outputtext> </body>
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!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment