Jsf-customconvertor-tag
提供:Dev Guides
JSF-カスタムコンバーター
JSFで独自のカスタムコンバーターを作成できます。
JSFでのカスタムコンバーターの定義は、3つのステップから成るプロセスです。
Step | Description |
---|---|
1 | Create a converter class by implementing javax.faces.convert.Converter interface. |
2 | Implement getAsObject() and getAsString() methods of above interface. |
3 | Use Annotation @FacesConvertor to assign a unique id to the custom convertor. |
ステップ1:コンバータークラスの作成:UrlConverter.java
ステップ2:コンバーターインターフェイスメソッドの実装:UrlConverter.java
データを保存する単純なクラスUrlDataを作成します。 このクラスはURL文字列を保存します。
getAsObjectメソッドでUrlDataを使用します。
ステップ3:コンバーターを登録するための注釈:UrlConverter.java
JSFページでコンバーターを使用する
応用例
上記のタグをテストするテストJSFアプリケーションを作成しましょう。
Step | Description |
---|---|
1 | Create a project with a name helloworld under a package com.finddevguides.test as explained in the JSF - First Application chapter. |
2 | Create UrlData.java under package com.finddevguides.test as explained below. |
3 | Create UrlConvertor.java as a converter under package com.finddevguides.test as explained below. |
4 | Create UserData.java as a managed bean under package com.finddevguides.test as explained below. |
5 | Modify home.xhtml as explained below. Keep rest of the files unchanged. |
6 | Create result.xhtml in the webapps directory as explained below. |
7 | Compile and run the application to make sure the business logic is working as per the requirements. |
8 | Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver. |
9 | Launch your web application using appropriate URL as explained below in the last step. |
UrlData.java
UrlConvertor.java
UserData.java
home.xhtml
result.xhtml
すべての変更を完了したら、JSF-最初のアプリケーションの章で行ったようにアプリケーションをコンパイルして実行します。 すべてがアプリケーションで問題ない場合、次の結果が生成されます。
無効な値を入力して、[送信]ボタンを押します。 次のエラーメッセージを参照してください。
有効な値を入力して、[送信]ボタンを押します。 次の結果を参照してください。