3

私の Smpring MVC アプリケーションでは、SimpleDateFormat を WebDataBinder のカスタム エディターとして使用して日付を検証しています。入力された日付が必要なパターンと一致しない場合、次のような form:errors タグに生のエラー メッセージが表示されます。

Failed to convert property value of type java.lang.String to required type java.util.Date for property hireDate; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "432345"

私の問題は、次のようなカスタムエラーメッセージをjspページに表示したいということです:

「生年月日は「dd/MM/yyyy」パターンと一致する必要があります」

これが私の @InitBinder のコードです:

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}

ありがとう。

4

2 に答える 2

4

メッセージ バンドルに次のメッセージを登録してみてください。

typeMismatch.command.field=Date of birth must match "dd/MM/yyyy" pattern
or
typeMismatch.field = ...

コマンドとフィールドを適切なコマンド オブジェクトに置き換えます

詳細はこちら: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/DefaultMessageCodesResolver.html

于 2014-05-22T00:48:44.260 に答える
0

これはあなたが探しているものではないかもしれませんが、コマンドでは (ほぼ) すべての属性に文字列を使用しています。

次に、これらの文字列に対して検証を実行して、コマンドにアクセスしたときに期待するデータ型になるかどうかを確認します。

于 2014-05-21T20:21:06.170 に答える