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>
正如您所看到的屬性文件我有幾個選擇,其中沒有被拿起時執行驗證。
你有這個什麼建議嗎?