簡體   English   中英

Spring 4 Hibernate驗證器本地化消息

[英]Spring 4 Hibernate validator localized messages

我嘗試使用自定義的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!

當我顯示驗證錯誤時,我看到標准消息“可能不是空”而不是我的自定義和本地化消息。 我做錯了嗎? 在此先感謝您的幫助,最誠摯的問候

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

如果這對您不起作用,請發表評論。 所以我可以進一步幫助你。

對於任何也有問題的人來說,驗證器消息是本地化的。 也許這有幫助。

我的本地化屬性文件位於src/main/resources/lang/ ,稱為validation.properties

@EnableWebMvcorg.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport導入Spring MVC配置,然后您可以覆蓋單個方法。 (有關詳細信息,請參閱API文檔

將其添加到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;
    }
}

我認為, 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM