Spring 4 Hibernate验证器本地化消息

时间:2014-03-11 19:00:48

标签: spring validation spring-mvc localization

我尝试使用自定义hibernate消息进行本地化工作,但我无法使其正常工作。 我验证的类看起来像这样:

@Entity
@Table(name = "USERS")
public class UserEntity extends AbstractBaseEntity implements UserDetails {

    @NotNull
    @Column(unique = true, nullable = false)
    @Size(min = 5, max = 30)
    private String username;

这是我弹簧配置的一部分:                   

<!-- Localization of hibernate messages during validation!-->
<bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:validation" />
</bean>

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

在资源中我有两个验证文件:validation_en.properties和validation_pl.properties以下是示例条目:

NotNull.UserEntity.username=Username can't be empty!

当我显示验证错误时,我看到标准消息&#34;可能不是null&#34;而不是我的自定义和本地化的。我做错了吗?在此先感谢您的帮助,最诚挚的问候

3 个答案:

答案 0 :(得分:3)

你需要做的是@NotNull(message = "{error.username.required}")

@Entity
@Table(name = "USERS")
public class UserEntity extends AbstractBaseEntity implements UserDetails {

@NotNull(message = "{error.username.required}")
@Column(unique = true, nullable = false)
@Size(min = 5, max = 30)
private String username;

资源文件夹中的validation_en.properties属性文件

error.username.required=Username can't be empty!

在春季配置中:

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

 <!-- Localization of hibernate messages during validation!-->
 <bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:validation" />
 </bean>

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

如果这对您不起作用,请发表评论。所以我可以帮助你。

答案 1 :(得分:2)

对于任何也有问题的人来说,本地化的验证器消息都是如此。也许这有帮助。

我的本​​地化属性文件位于src/main/resources/lang/,名为validation.properties

@EnableWebMvcorg.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport导入Spring MVC配置,然后您可以覆盖单个方法。 (有关详细信息,请参阅API doc

将此添加到Java Spring配置文件中,您的验证消息应正确本地化:

@Configuration
@EnableWebMvc
@ComponentScan
public class MvcWebConfig extends WebMvcConfigurerAdapter {

// other configurations may be here ...

    @Bean( name = "validationMessageSource" )
    public ReloadableResourceBundleMessageSource validationMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:lang/validation");
        messageSource.setCacheSeconds(10); // reload messages every 10 seconds
        return messageSource;
    }

    @Override
    public Validator getValidator() {
       LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
       validator.setValidationMessageSource((MessageSource) validationMessageSource());
       return validator;
    }
}

答案 2 :(得分:0)

我认为,validationMessageSource找不到文件:

  • 当您调整src/main.webapp/WEB-INF文件夹中的文件时,请删除前置/WEB-INF/validation代替/WEB-INF/validation):

  • 将文件放入资源文件夹时,请使用classpath前缀:classpath:validation

例如:

<bean id="validationMessageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
      <property name="basename" value="classpath:validation" />
</bean>