Domyślne komunikaty sprawdzania poprawności wiosny
Muszę programowo uzyskać rozwiązane komunikaty o błędach w kontrolerze. Domyślny komunikat sprawdzania poprawności dla błędów typeMismatch nie jest zapełniany z mojego pliku messages.properties. Mam obiekt kopii zapasowej formularza, w którym pole jest liczbą całkowitą. Jeśli prześlę ciąg dla tego pola, otrzymam:
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"
jako domyślny komunikat w ObjectError. Oto mój kontroler, który go wyświetla:
@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 ...
}
Więc dodałem messages.properties do WEB-INF / klas z niektórymi komunikatami testowymi, aby sprawdzić, czy mogę przesłonić tę domyślną wiadomość:
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
W moim pliku app-servlet.xml mam:
<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>
Dlaczego nie odbiera żadnych moich wiadomości z mojego pliku messages.properties?