Home > Java > javaTutorial > body text

JSF Converters

WBOY
Release: 2024-08-30 15:14:11
Original
572 people have browsed it

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 Series

Start 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"/>
Copy after login

f:convertNumber – This tag is used to add a “NumberConverter” instance to the parent component.

Example 

<c:convertNumber type = "javax.faces.Integer"/>
Copy after login

f:convertDateTime – This tag is used to add a “DateTimeConverter” instance to the parent component.

Example 

<f:convertDateTime pattern = "dd/mm/yyyy"/>
Copy after login

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" />
Copy after login
Copy after login

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" />
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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;
}
}
Copy after login

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>
Copy after login

An output of the above project in the sequence is –

JSF Converters

You fill the details as below –

JSF Converters

Once you click the button the output is –

JSF Converters

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template