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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

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

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

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.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.
