標準のJSF変換および検証エラー・メッセージは、詳細、技術的、またはいつかは、人間が読めるものではありません。この記事では、JSF 2.0で標準の変換または検証エラーメッセージをカスタマイズする方法を示します。


Summary Guide

  1. jsf-api-2.x.jarからメッセージキーを探します. “Messages.properties”

ファイル。

  1. 独自のプロパティファイルを作成し、同じメッセージキーを置く

上記の “Messages.properties”ファイルで見つけられ、カスタムエラーメッセージで上書きします。

  1. プロパティファイルを “faces-config.xml”に登録し、

アプリケーションレベル。

  1. 完了しました.

1. Messages.properties

すべてのJSF標準変換と検証のエラーメッセージは、

jsf-api-2.x.jar

、 ”

javax \ faces \ Messages.properties

“にある「Messages.properties」ファイルに格納されます。下の図を参照してください。


jsf2  - カスタム検証 - エラー - 例-1、title = "jsf2  - カスタム検証 - エラー-Example-1 "、width = 549、height = 480

この ”

Messages.properties

“ファイルの部分を参照

...

#============================================= ==================#コンバータエラー#========================== ================================== javax.faces.converter.DateTimeConverter.DATE = { 2}: '' {0} 'は日付として理解できませんでした。

javax.faces.converter.DateTimeConverter.DATE__detail = {2}: '' {0} ''は日付として理解できませんでした。例:{1}
...
#============================================= =================
#バリデータのエラー
#============================================= =================
javax.faces.validator.LengthValidator.MAXIMUM = {1}:検証エラー:長さが許容可能な最大値 '' {0} ''より大きい
javax.faces.validator.LengthValidator.MINIMUM = {1}:検証エラー:長さが '' {0} ''の許容最小値未満です
...

For example,


1. <f:validateLength minimum=”5′′ maximum=”10′′/>


If maximum length validation failed, JSF gets
“javax.faces.validator.LengthValidator.MAXIMUM”.

If minimum length validation failed, JSF gets
“javax.faces.validator.LengthValidator.MINIMUM”.

2. Custom Error Message

Create a properties file named “

MyMessage.properties

” (can be any name
you like), put message key and custom error message inside. Later, put
this properties file into your project resources folder.

  • MyMessage.properties**

javax.faces.converter.DateTimeConverter.DATE = {2}: '' {0} ''は日付として理解できませんでした。

javax.faces.converter.DateTimeConverter.DATE__detail =日付フォーマットが無効です。

javax.faces.validator.LengthValidator.MINIMUM =「{0}」の最小長が必要です。

Now, you are going to custom the validation error message for


javax.faces.validator.LengthValidator.MINIMUM

” and conversion error
message for “

javax.faces.converter.DateTimeConverter.DATE__detail

“.

  • Note**

    For the XXX

    detail message key, you have to override its parent key
    (summary message) as well, which is XXX without the “

    detail” behind;
    Otherwise, JSF will ignore your new custom error message and keep
    getting the standard error message from “Messages.properties”, may be
    this is a bug in JSF 2.0?

3. Register Message Bundle

Register your custom properties file in “faces-config.xml”, put it as
Application level.

  • faces-config.xml**

<?xml version = "1.0" encoding = "UTF-8"?>
<faces-config
    xmlns = "http://java.sun.com/xml/ns/javaee"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-facesconfig__2__0.xsd "
    version = "2.0">
     <アプリケーション>
      <message-bundle>
        com.mkyong.MyMessage
      </message-bundle>
     </application>
</faces-config>

4.デモ

JSFページで、

<f:validateLength/>



<f:convertDateTime/>

の両方を追加します。
検証。

<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE html PUBLIC " - //W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xmlns:h = "http://java.sun.com/jsf/html" xmlns/DTD/xhtml1-transitional.dtd "> <html xmlns =" http://www.w3.org/1999/xhtml " <h:form> <h:body> <h1> JSF 2.0で検証エラーメッセージをカスタマイズする</h1> <h:form> <h:panelGrid columns = " 3 ">ユーザー名を入力してください:

<h:inputText id = "username" value = "#{user.username}" size = "20" required = "true" label = "ユーザー名"> <f:validateLength minimum = "5" maximum = "10"/> </h:inputText> <h:message for = "username" style = "color:red"/>あなたのDOBを入力してください:

<h:inputText id = "dob" value = "#{user.dob}"
                size = "20" required = "true" label = "生年月日">
                <f:convertDateTime/>
            </h:inputText>
                
            <h:message = "dob" style = "color:red"/>
                
        </h:panelGrid>
            
        <h:commandButton value = "Submit" action = "result"/>
            
    </h:form>
    </h:body>
</html>

検証に失敗したら、今度はカスタムエラーメッセージを表示してください。


jsf2-Custom-Validation-Error-Example-2、title = "jsf2-Custom-Validation-Error" -Example-2 "、width = 640、height = 299

===ソースコードをダウンロードする

それをダウンロードする –

JSF-2-Custom-Validation-Error-Message-Example.zip

(11KB)

===リファレンス

。 link://jsf2/jsf-2-0-and-resource-bundles-example/[JSF 2