首页 > Java > java教程 > 正文

JSF 转换器

WBOY
发布: 2024-08-30 15:14:11
原创
533 人浏览过

在 JSF (JavaServer Faces) 应用程序中,用户输入使用 http 请求作为客户端请求发送到服务器。这些用户输入称为值。这些请求值以字符串的形式发送到服务器。然而,JSF 应用程序使用各种数据类型,例如 int、float、double、String、Boolean、date 等。因此,在处理请求值之前需要将其转换为适当的数据类型。这个转变过程称为转换。在本主题中,我们将了解 JSF 转换器。

广告 该类别中的热门课程 JSF Java Server Faces - 学习路径 | 6门课程系列

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

为了在 JSF 应用程序中完成转换,JSF 框架提供了标准转换器。这些转换器作为 JSF 核心标签库中的标签提供。此外,您还可以创建自己的转换器来完成应用程序要求。  这些转换器称为自定义转换器。

转换器标签

在 JSF 应用程序中,用户输入到 UI 组件中的数据需要转换为适当的格式,然后才能由应用程序处理。下表列出了 JSF 核心标签库提供的用于执行转换的标准标签。

用于数据转换的 JSF 核心标签 –

f:converter – 此标签用于向实例父组件添加任意转换器。

示例

<f:converter converterId = "javax.faces.Integer"/>
登录后复制

f:convertNumber – 此标签用于向父组件添加“NumberConverter”实例。

示例

<c:convertNumber type = "javax.faces.Integer"/>
登录后复制

f:convertDateTime – 此标签用于向父组件添加“DateTimeConverter”实例。

示例

<f:convertDateTime pattern = "dd/mm/yyyy"/>
登录后复制

JSF 框架提供了数字和日期的标准转换器。有时在应用程序中,我们需要将用户输入转换为数字和日期数据类型,因此我们可以使用 JSF 框架提供的标准转换器。所有标准转换器都包含在 JSF 框架的 javax.faces.convert 包中。所有转换器都是根据组件的值隐式应用的,如果我们想访问这些转换器,我们可以通过转换器 Id 来访问它们。

JSF 框架转换器

IntegerConverter 类,用于将用户输入的字符串值转换为 java.lang.Integer 类型的值,其转换器 id 为 javax.faces.Integer.

示例

<h:inputText id="age" converter="javax.faces.Integer" />
登录后复制
登录后复制

BigIntegerConverter 类,用于将用户输入的字符串值转换为 java.lang.BigInteger 类型的值,其转换器 id 为 javax.faces.BigInteger。

示例

<h:inputText id="age" converter="javax.faces.Integer" />
登录后复制
登录后复制

相同的方法可以用于不同的整数类型。

ShortConverter 类,用于将用户输入的字符串值转换为 java.lang.Short 类型的值,其转换器 id 为 javax.faces.Short。

LongConverter 类,用于将用户输入的字符串值转换为 java.lang.Short 类型的值,其转换器 id 为 javax.faces.Long。

NumberConverter 类,用于将用户输入的字符串值转换为 java.lang.Number 类型的值,其转换器 id 为 javax.faces.Number。

示例

<h:outputText value="#{userBean.height}">
<f:convertNumber maxFractionDigits="2" />
</h:outputText>
登录后复制

FloatConverter 类,用于将用户输入的字符串值转换为 java.lang.Float 类型的值,其转换器 id 为 javax.faces.Float。

BigDecimalConverter 类,用于将用户输入字符串值转换为 java.lang。 BigDecimal 值类型及其转换器 ID 为 javax.faces.BigDecimal。

DoubleConverter 类,用于将用户输入字符串值转换为 java.lang。 Double 类型的值,其转换器 id 为 javax.faces.Double。

ByteConverter 类,用于将用户输入的字符串值转换为 java.lang.Byte 类型的值,其转换器 id 为 javax.faces.Byte。

CharacterConverter 类,用于将用户输入的字符串值转换为 java.lang。值的字符类型及其转换器 id 是 javax.faces.Character.

BooleanConverter 类,用于将用户输入的字符串值转换为 java.lang.Boolean 类型的值,其转换器 id 为 javax.faces.Boolean。

DateTimeConverter 类,用于将用户输入字符串值转换为 java.lang。 DateTime 值类型及其转换器 ID 为 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 –

JSF 转换器

You fill the details as below –

JSF 转换器

Once you click the button the output is –

JSF 转换器

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.

以上是JSF 转换器的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!