コントローラーでプログラムによって解決されたエラー メッセージを取得する必要があります。typeMismatch エラーのデフォルトの検証メッセージが、messages.properties ファイルから読み込まれません。フィールドが整数であるフォーム バッキング オブジェクトがあります。そのフィールドに文字列を送信すると、次のようになります。
Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'establishedYear'; nested exception is java.lang.NumberFormatException: For input string: "1995a"
ObjectError のデフォルト メッセージとして。これを出力するコントローラーは次のとおりです。
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody FormJSONResponse postForm(@Valid ProfileEditCompanyForm profileEditCompanyForm, BindingResult result) throws Exception {
if (result.hasErrors()) {
for (ObjectError objectError : result.getAllErrors()) {
System.out.println(objectError.getDefaultMessage()); // THIS IS NOT MY MESSAGE, BUT SHOULD BE
}
}
... other stuff ...
}
そこで、messages.properties を WEB-INF/classes にいくつかのテスト メッセージとともに追加して、そのデフォルト メッセージをオーバーライドできるかどうかを確認しました。
typeMismatch.profileEditCompanyForm.establishedYear=test 1
typeMismatch.establishedYear=test 2
typeMismatch.java.lang.Integer=test 3
typeMismatch=test 4
profileEditCompanyForm.establishedYear=test 5
establishedYear=test 6
私の app-servlet.xml ファイルには次のものがあります。
<mvc:annotation-driven conversion-service="conversionService" validator="validator"/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource"/>
</bean>
messages.properties ファイルからメッセージが取得されないのはなぜですか?