无法让Hibernate Validator与Spring MessageSource一起使用

Rus*_*ell 6 validation hibernate localization spring-mvc

我正在尝试让Hibernate Validator设置为使用来自Spring MessageSource的消息.我在我的设置中有以下设置messages-context.xml:

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>WEB-INF/messages/messages</value>
            <value>WEB-INF/messages/exceptions</value>
            <value>WEB-INF/messages/fields</value>
            <value>WEB-INF/messages/buttons</value>
            <value>WEB-INF/messages/validation_errors</value>
        </list>
    </property>
</bean>

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

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

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="en_GB" />
</bean>
Run Code Online (Sandbox Code Playgroud)

我已经尝试了各种方法来解决如何将消息密钥传递给hibernate验证器(包括和不包含{},也没有指定自定义密钥 - 只使用默认密钥:

@NotEmpty(message="validation.required.field")
@Length(max=255, message="{validation.too.long}")
private String firstName;

@NotEmpty(message="{validation.required.field}")
@Length(max=255, message="{validation.too.long}")
private String lastName;

@NotNull
@Past(message="{validation.must.be.past}")
private Date dateOfBirth;

@NotEmpty(message="{validation.required.field}")
@Length(max=255, message="{validation.too.long}")
private String email;
Run Code Online (Sandbox Code Playgroud)

validation_errors_en_GB.properties看起来像这样:

validation.required.field=this is a required field
validation.too.long=this field can only take {0} characters
validation.must.be.past=this date has to be in the past 
javax.validation.constraints.NotNull.message=Custom message
Run Code Online (Sandbox Code Playgroud)

但是,验证空字段时,显示的消息如下:

First name      validation.required.field
Last name       {validation.required.field}
Date of birth   may not be null
Email           {validation.required.field}
Run Code Online (Sandbox Code Playgroud)

无论出于何种原因,始终使用消息的密钥 - 实际消息永远不会被查找.知道我哪里错了吗?

谢谢,

罗素

Tom*_*yre 13

这让我感到困惑了一段时间,但问题是你需要向Spring注册用于验证@Controller方法的Validator(感谢这个洞察力的答案!)

因此,如果您使用XML配置,请执行以下操作:

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

<mvc:annotation-driven validator="validator"/> 
Run Code Online (Sandbox Code Playgroud)

如果您使用的是javaconfig,请执行以下操作:

@EnableWebMVC
@Configuration
public MyWebAppContext extends WebMvcConfigurerAdapter {

@Bean
public LocalValidatorFactoryBean validator() {
    LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
    validatorFactoryBean.setValidationMessageSource(messageSource);
    return validatorFactoryBean;
}

@Override
public Validator getValidator() {
    return validator();
}
Run Code Online (Sandbox Code Playgroud)

(参见Spring Web MVC框架文档)


Mar*_*arc 1

Hibernate Validator 正在不同的位置寻找区域设置。尝试这个:

LocaleContextHolder.setLocale(locale);