【问题标题】:Spring 4 Hibernate validator localized messagesSpring 4 Hibernate 验证器本地化消息
【发布时间】:2014-03-11 19:00:47
【问题描述】:

我尝试使用自定义休眠消息进行本地化,但无法使其正常工作。 我经过验证的课程如下所示:

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

这是我的 spring 配置的一部分:

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

当我显示验证错误时,我看到标准消息“可能不是 null”,而不是我的自定义和本地化消息。我做错了什么?提前感谢您的帮助,最好的问候

【问题讨论】:

  • 验证文件在哪里?
  • 好点。我将文件放在资源(src/main/resources)下。我更新了我的 bean 配置,但仍然无法正常工作。

标签: spring validation spring-mvc localization


【解决方案1】:

你需要做的是@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!

在spring配置中:

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

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

【讨论】:

  • 谢谢! IDEA 出于某种原因定义了没有 { } 的属性,但没有定义带有 { } 的属性。这让人困惑
【解决方案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;
    }
}

【讨论】:

    【解决方案3】:

    我认为,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>
    

    【讨论】:

    • 我更改了它,但它仍然不加载我的自定义消息。
    • 要检查文件是否已加载,您可以尝试直接访问验证消息密钥。 - 只是为了确保消息已加载,并且您没有在错误的位置搜索问题。
    • 在某处使用消息键 - 无论如何 - 请参阅我修改后的答案:问题是类路径前缀
    • 我已经将其更改为 classpath:validation 但我仍然收到默认错误消息
    • 那么可能还有一些问题。
    猜你喜欢
    • 2016-10-09
    • 2016-12-06
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    相关资源
    最近更新 更多