Umesh Awasthi
Umesh Awasthi

Reputation: 23587

Spring-mvc validation error not being picked from property file

Using spring-mvc build in JSR303 bean validation and its working fine except for one issue, messages are not being picked from property file.

My Web-application is being created with maven and this is current structure

Main
      -java
      -resources
         -bundles
            -message.properties
      -webapp

XML file

<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <beans:property name="basenames">
    <beans:list>
    <beans:value>bundles/messages</beans:value>
    <beans:value>bundles/shipping</beans:value>
    <beans:value>bundles/payment</beans:value> 
     </beans:list>
  </beans:property>
 </beans:bean>

-- validator

<beans:bean id="validator"   
   class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
  <beans:property name="validationMessageSource" ref="messageSource"/>
</beans:bean>

annotation-driven

<annotation-driven>
  <message-converters>
            <beans:bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
                <beans:property name="supportedMediaTypes">
                    <beans:list>
                        <beans:value>image/jpeg</beans:value>
                        <beans:value>image/gif</beans:value>
                        <beans:value>image/png</beans:value>
                    </beans:list>
                </beans:property>
            </beans:bean>
        </message-converters>
</annotation-driven>

Java File

@NotEmpty(message="{registration.firstName.invalid}")
  public String getFirstName() {
   return firstName;
}

Some how on my JSP page, I am getting these messages This field is required, not sure what is issue My Data class is having following structure

PersistableCustomer extends SecuredCustomer
SecuredCustomer extends CustomerEntity

Even after passing message source to validator, its not picking up message from custom property file.

Upvotes: 1

Views: 6822

Answers (1)

Pavel Horal
Pavel Horal

Reputation: 18183

I am taking a wild guess here... usually JSR-303 message interpolator is taking messages from ValidationMessages.properties. If you want your validator to use Spring's message source, you need to configure it that way:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource" ref="messageSource" />
</bean>

<mvc:annotation-driven validator="validator" />

Upvotes: 3

Related Questions