2015-12-21 126 views
1

我越来越绝望我试图配置验证错误消息来自消息源。Spring MVC i18n验证错误消息

我有一个SignupForm.java类如:

public class SignupForm { 

    @NotBlank 
    @Size(min = 3, max = 32) 
    private String username; 

    ... 
} 

的消息位于:src/main/resources/i18n

-- message_en.properties 
-- message_zh.properties 
-- validation_en.properties 
-- validation_zh.properties 

里面validation_en.properties我有:

org.hibernate.validator.constraints.NotEmpty.message = Default message, can not be empty 
NotEmpty.username  = Username cannot be blank. 
Size.SignupForm.username = Username must be between {2} and {1} characters long. 

MessageSource配置:

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

Validator配置:

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

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

正如您所看到的属性文件我有几个选择,其中没有被拿起时执行验证。

validation form

你有这个什么建议吗?

回答

1

问题是我的i18n解析器bean的语言环境设置为cn。将其更改为zh解决了该问题。麻烦的是,它会默认没有任何错误。