I'll be brief.

There is a controller with validation:

@RequestMapping(method = RequestMethod.POST) public @ResponseBody ValidationResponse addOrder(@ModelAttribute(value = PARAM_NAME) @Valid Orders orders, BindingResult bindingResult) { ... } 

When you type on the form of inconsistency type "asda" in the field for java.lang.Long , I get the corresponding Exception about the inability to convert from string to number. The message of this Exception is stored in bindingResult

How can I change the message that appears on its own, internationalized in accordance with the locale?

What I tried: In the spring config there is such a thing:

 <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/WEB-INF/messages" /> <property name="defaultEncoding" value="UTF-8"/> </bean> <bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="validationMessageSource"> <ref bean="messageSource"/> </property> </bean> 

The /WEB-INF/messages.properties has a bunch of lines:

 Orders.width.NotNull=Это поле не может быть пустым! Orders.height.NotNull=Это поле не может быть пустым! typeMismatch.java.util.Date=Должно быть коррекной датой typeMismatch.java.lang.Integer=Должно быть корректным числом typeMismatch.java.lang.Long=Должно быть корректным числом java.lang.NumberFormatException=Должно быть корректным числом typeMismatch.java.lang.NumberFormatException=Должно быть корректным числом typeMismatch.java.lang.NumberFormat=Должно быть корректным числом typeMismatch.orderAdd.width=Должно быть корректным числом typeMismatch=Должно быть корректным числом 

But as you probably guessed, everything is under the tail of the cat and it doesn’t rezolvitsya. Climbed a bunch of sites, but did not find the answer.

Who can knows, or faced this problem will prompt the decision?

It is noteworthy that if for a field, for example, width put a check on Null, for example:

 ... @NotNull(message="{Orders.width.NotNull}") private Long width; ... 

That check messages on Null rezolvitsya normally. But this does not affect the conversion error.

  • In which Spring messageSource your bean messageSource ? - Slava Semushin 9:09
  • In /WEB-INF/action-servlet.xml . And in the web.xml there are lines: ... <servlet> <servlet-name> action </ servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </ servlet-class> <init-param > <param-name> contextConfigLocation </ param-name> <param-value> /WEB-INF/action-servlet.xml </ param-value> </ init-param> <load-on-startup> 1 </ load-on-startup> </ servlet> ... - delphist007

1 answer 1

In general, who are interested, I found a more or less suitable solution. Apparently, if you receive messages in this way:

 List<FieldError> allErrors = result.getFieldErrors(); for (FieldError objectError : allErrors) { objectError.getDefaultMessage(); } 

So for most of the errors, my messageSource not used (and really, why should I?), So you need to translate this business almost manually, in such a straightforward way:

 messageSource.getMessage(objectError, locale) 

At the output we get a string from messageSource .

But if someone finds a better solution, I will be grateful.