1

I need to get the resolved error messages programmatically in the controller. The default validation message for typeMismatch errors are not populating from my messages.properties file. I have a form backing object where a field is an Integer. If I submit a string for that field I get:

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"

as the default message in the ObjectError. Here's my controller that output it:

  @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 ...
  }

So I added a messages.properties to WEB-INF/classes with some test messages to see if I could override that default message:

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

In my app-servlet.xml file I have:

<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>

Why isn't it picking up any of my messages from my messages.properties file?

Jeremy
  • 1,023
  • 3
  • 18
  • 33

3 Answers3

5

Try this in you Spring context instead:

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="WEB-INF/classes/messages" />
</bean>

Then inside "WEB-INF/classes" folder create a file call: "messages.properties"

Take note for the content of "messages.properties" you have to provide it like this :

typeMismatch.pathValueInsideYourJSPform:input= Your Message 

Hope this helps you !

here is a sample also

Community
  • 1
  • 1
Mehdi
  • 3,795
  • 3
  • 36
  • 65
0

Try specifying the complete path and try

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
      <property name="basename" value="WEB-INF/messages" />
    </bean>
Usha
  • 1,458
  • 11
  • 13
  • Doesn't work. I've tried everything I can think of in that value attribute and I now have my messages.properties file littered all over the place hoping it would pick up something. – Jeremy Nov 09 '12 at 22:11
0

Apparently I have to run the FieldError objects through the Spring MessageSource. I was hoping this was done automatically. I found my answer here:

How to get error text in controller from BindingResult

Community
  • 1
  • 1
Jeremy
  • 1,023
  • 3
  • 18
  • 33
  • If you are using the Update answer given by Arthur then it means he is checking the validations in the customed validator.. so you do not need your bean id with validator...If your using the answer above the Update answer then you require your bean id validator in servlet.xml..Is it correct?..am i following?.. – Pratap M Nov 12 '12 at 13:56
  • my question is that why you use org.springframework.validation.beanvalidation.LocalValidatorFactoryBean...so that does it do validations by itself..? – Pratap M Nov 12 '12 at 14:19