没有从属性文件中选择Spring-MVC验证错误 [英] Spring-mvc validation error not being picked from property file

查看:105
本文介绍了没有从属性文件中选择Spring-MVC验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JSR303 bean验证中的spring-mvc构建及其正常工作(除了一个问题),不会从属性文件中提取消息.

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

我的Web应用程序正在使用maven创建,这是当前结构

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

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

XML文件

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

- 验证器

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

注释驱动

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

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

在我的JSP页面上,如何得到这些消息This field is required,不确定是什么问题 我的数据类具有以下结构

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.

推荐答案

我在这里大胆猜测...通常JSR-303消息插值器正在从ValidationMessages.properties接收消息.如果您希望验证程序使用Spring的消息源,则需要通过以下方式进行配置:

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

这篇关于没有从属性文件中选择Spring-MVC验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆