JSFコンバータ

WBOY
リリース: 2024-08-30 15:14:11
オリジナル
502 人が閲覧しました

JSF (JavaServer Faces) アプリケーションでは、ユーザー入力はクライアント要求として http 要求を使用してサーバーに送信されます。これらのユーザー入力は値と呼ばれます。これらのリクエスト値は文字列の形式でサーバーに送信されます。ただし、JSF アプリケーションは、int、float、double、String、Boolean、date などのさまざまなデータ型を使用します。したがって、リクエスト値が処理される前に、適切なデータ型に変換する必要があります。この変換プロセスは変換と呼ばれます。このトピックでは、JSF コンバーターについて学習します。

広告 このカテゴリーの人気コース JSF Java Server Faces - ラーニング パス | 6コースシリーズ

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

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 フレームワークを使用して標準コンバータを提供できるように、ユーザー入力を数値および日付データ型に変換する必要がある場合があります。すべての標準コンバータは、javax.faces.convertpackage JSF フレームワークに含まれています。すべてのコンバーターはコンポーネントの値に基づいて暗黙的に適用され、これらのコンバーターにアクセスしたい場合は、コンバーター ID によってアクセスできます。

JSF フレームワーク コンバータ

ユーザー入力文字列値を java.lang.Integer 型の値に変換するために使用する IntegerConverter クラス。そのコンバータ ID は javax.faces.Integer.

<h:inputText id="age" converter="javax.faces.Integer" />
ログイン後にコピー
ログイン後にコピー

ユーザー入力文字列値を java.lang.BigInteger 型の値に変換するために使用する BigIntegerConverter クラス。そのコンバータ ID は javax.faces.BigInteger.

<h:inputText id="age" converter="javax.faces.Integer" />
ログイン後にコピー
ログイン後にコピー

同じ方法をさまざまな整数型に使用できます。

ユーザー入力文字列値を java.lang.Short タイプの値に変換するために使用する ShortConverter クラス。そのコンバータ ID は javax.faces.Short です。

ユーザー入力文字列値を java.lang.Short タイプの値に変換するために使用する LongConverter クラス。そのコンバータ ID は javax.faces.Long です。

ユーザー入力文字列値を java.lang.Number タイプの値に変換するために使用する NumberConverter クラスとそのコンバータ ID は javax.faces.Number です。

<h:outputText value="#{userBean.height}">
<f:convertNumber maxFractionDigits="2" />
</h:outputText>
ログイン後にコピー

ユーザー入力文字列値を java.lang.Float タイプの値に変換するために使用する FloatConverter クラスと、そのコンバータ ID は javax.faces.Float です。

ユーザー入力文字列値を java.lang に変換するために使用する BigDecimalConverter クラス。 BigDecimal の値のタイプとそのコンバータ ID は javax.faces.BigDecimal です。

ユーザー入力文字列値を java.lang に変換するために使用する DoubleConverter クラス。 Double 型の値とそのコンバータ ID は javax.faces.Double です。

ユーザー入力文字列値を java.lang.Byte タイプの値に変換するために使用する ByteConverter クラスと、そのコンバータ ID は javax.faces.Byte です。

ユーザー入力文字列値を java.lang に変換するために使用する CharacterConverter クラス。値の文字型とそのコンバータ ID は javax.faces.Character.

です。

ユーザー入力文字列値を java.lang.Boolean 型の値に変換するために使用する BooleanConverter クラス。そのコンバータ ID は javax.faces.Boolean です。

ユーザー入力文字列値を java.lang に変換するために使用する DateTimeConverter クラス。 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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!