【问题标题】:Hibernate validator could not get value by keyHibernate 验证器无法通过键获取值
【发布时间】:2016-01-13 01:34:29
【问题描述】:

我的项目使用 spring mvc 和 hibernate 验证器。我正在努力使其支持多语言。 这是我的 spring-i18N 文件

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <mvc:interceptors>
        <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang"/>
        </bean>
    </mvc:interceptors>


    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="cookieMaxAge" value="604800"/>
        <property name="defaultLocale" value="zh_CN" />
        <property name="cookieName" value="language"></property>
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:i18N/messages</value>
                <value>classpath:i18N/validation</value>
            </list>
        </property>
           <property name="defaultEncoding" value="UTF-8" />
        <property name="useCodeAsDefaultMessage" value="false"/>
    </bean>

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

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

我的问题是,当我在 bean 文件中使用注释时,像这样,

@Size(min=6, max=25,message="{password_length_require}")
private String password;

在validation_en_US.properties中,我这样做

password_length_require=The password length require more than 5 and less 25

结果是该页面将输出“{password_length_require}”。 另一方面,在我的控制器中,我使用

bindingResult.rejectValue("password", "password_length_require");

它会正常工作的......

谁能帮帮我?

【问题讨论】:

    标签: spring-mvc hibernate-validator


    【解决方案1】:

    我以前也遇到过同样的问题,我花了很长时间才解决它。 和我一样,下面的配置可能有问题。

    <property name="basenames">
            <list>
                <value>classpath:i18N/messages</value>
                <value>classpath:i18N/validation</value>
            </list>
        </property>
    

    属性“basenames”的值必须是验证消息文件在项目中保存的完整路径。例如,如果验证消息文件在名为“org.example.config.i18N.messages”的包中,则应配置像这样的xml:

    <value>classpath:org/example/config/i18N/messages</value>
    

    所以也许你需要检查你的配置是否正确。 以上是我的经验,希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多