Announcement Announcement Module
Collapse
No announcement yet.
MessageSource not being used during validation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MessageSource not being used during validation

    I can't get my messages in messages.properties to be used during Spring validation of my form backing objects.

    app-config.xml:
    Code:
      <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages" />
      </bean>
    WEB-INF/classes/messages.properties:
    Code:
    NotEmpty=This field should not be empty.
    Form Backing Object:
    Code:
    ...
      @NotEmpty
      @Size(min=6, max=25)
      private String password;
    ...
    When I loop through all errors in the BindingResult and output the ObjectError's toString I get this:

    Field error in object 'settingsForm' on field 'password': rejected value []; codes [NotEmpty.settingsForm.password,NotEmpty.password,N otEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessage SourceResolvable: codes [settingsForm.password,password]; arguments []; default message [password]]; default message [may not be empty]

    As you can see the default message is "may not be empty" instead of my message "This field should not be empty".

    I do get my correct message if I inject the messageSource into a controller and output this:
    messageSource.getMessage("NotEmpty", new Object [] {"password"}, "default empty message", null);

    So why isn't Spring's validation using my messages.properties? I'm running Spring 3.1.1. Thanks!
    Last edited by neojonas; Jun 20th, 2012, 04:10 PM.

  • #2
    Try to define the messageSource in the servlets context instead of the root context (although it should work). Try to set the log level to debug/trace so that you see what happens (especially during the startup of the servlet) you should see whether the MessageSource is resolved or not (and if it finds the messages.properties).

    Comment


    • #3
      Thanks for the response. I have it using my messages.properties file...kind of. I added a validator to my mvc:annotation-driven tag:

      Code:
        <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>
      So now it picks up my message in messages.properties if it's set like this:
      org.hibernate.validator.constraints.NotEmpty.messa ge=This is an empty field

      But I can't figure out how to pick up a message for a specific field without having to explicitly define it by adding a message attribute to my validation annotation. I would like to have a message defined like this be picked up automatically when I have a password field annotated with @NotEmpty:

      NotEmpty.password=Password field can not be empty

      Ideas?
      Last edited by neojonas; Jun 21st, 2012, 12:23 PM.

      Comment

      Working...
      X