2015-12-21 238 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解決了該問題。麻煩的是,它會默認沒有任何錯誤。