Spring-mvc validation error not being picked from property file

爷,独闯天下 提交于 2019-12-11 06:58:14

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/21508610/spring-mvc-validation-error-not-being-picked-from-property-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!