12億円ほしい人のブログ

メガBIG当たらないかな

BindingResultとth:errorclass

Spring Bootで、コントローラーの引数にBindingResultを用意すると、 画面入力値→Javaクラスへのバインディングエラーや入力値チェックエラーの情報を取得できるようです。

Thymeleafにて、そのエラー情報を使うことができますが、その仕組みがよくわからないです。 BindingResultやModelをコンソールにprintlnしてみました。

出力結果

bindingResult結果:org.springframework.validation.BeanPropertyBindingResult: 2 errors Field error in object 'signupForm' on field 'age': rejected value [a]; codes [typeMismatch.signupForm.age,typeMismatch.age,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [signupForm.age,age]; arguments ; default message [age]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'age'; nested exception is java.lang.NumberFormatException: For input string: "a"]

Field error in object 'signupForm' on field 'birthday': rejected value [a]; codes [typeMismatch.signupForm.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [signupForm.birthday,birthday]; arguments ; default message [birthday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthday'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date] for value 'a'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [a]]

model中身:{signupForm=SignupForm(userId=, password=, userName=, birthday=null, age=null, gender=null), org.springframework.validation.BindingResult.signupForm=org.springframework.validation.BeanPropertyBindingResult: 2 errors Field error in object 'signupForm' on field 'age': rejected value [a]; codes [typeMismatch.signupForm.age,typeMismatch.age,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [signupForm.age,age]; arguments ; default message [age]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'age'; nested exception is java.lang.NumberFormatException: For input string: "a"]

Field error in object 'signupForm' on field 'birthday': rejected value [a]; codes [typeMismatch.signupForm.birthday,typeMismatch.birthday,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [signupForm.birthday,birthday]; arguments ; default message [birthday]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthday'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.util.Date] for value 'a'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [a]]}

なんとなく、以下の仕組みなのかと思いました
Model中身を見て赤字箇所の情報をThymeleaf側で検知
→nameが一致するfieldにエラー発生と解釈
→th:errorclassが動く

追記
エラー発生時に.class属性を追加したい、ということがよくあるらしく、より簡単に実装できるように追加されたのが、
th:errorclass
みたいです。

Tutorial: Thymeleaf + Spring

なので、#fieldsの処理と内部的には同じような感じなのでしょうか。