Jsf-customvalidator-tag
提供:Dev Guides
JSF-カスタム検証
JSFで独自のカスタムバリデーターを作成できます。
JSFでのカスタムバリデーターの定義は、3ステップのプロセスです。
Step | Description |
---|---|
1 | Create a validator class by implementing javax.faces.validator.Validator interface. |
2 | Implement validate() method of the above interface. |
3 | Use Annotation @FacesValidator to assign a unique ID to the custom validator. |
ステップ1:検証クラスを作成する:UrlValidator.java
ステップ2:バリデーターインターフェースメソッドの実装:UrlValidator.java
ステップ3:バリデーターを登録するための注釈:UrlValidator.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 UrlValidator.java as a converter under package com.finddevguides.test as explained below. |
3 | Create UserData.java as a managed bean under package com.finddevguides.test as explained below. |
4 | Modify home.xhtml as explained below. Keep rest of the files unchanged. |
5 | Create result.xhtml in the webapps directory as explained below. |
6 | Compile and run the application to make sure business logic is working as per the requirements. |
7 | Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver. |
8 | Launch your web application using appropriate URL as explained below in the last step. |
UrlValidator.java
UserData.java
home.xhtml
result.xhtml
すべての変更を完了したら、JSF-最初のアプリケーションの章で行ったようにアプリケーションをコンパイルして実行します。 すべてがアプリケーションで問題ない場合、次の結果が生成されます。
無効な値を入力して、[送信]ボタンを押します。 次のエラーメッセージを参照してください。
有効な値を入力して、[送信]ボタンを押します。 以下が出力です。