首頁 > Java > java教程 > 主體

JSF 轉換器

WBOY
發布: 2024-08-30 15:14:11
原創
501 人瀏覽過

在 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學習者快速成長!