我越来越没有希望尝试将验证错误消息配置为来自消息源。
我有一个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>
如您在属性文件中看到的,我有几个选项,并且在执行验证时没有一个被选中。
您对此有什么建议吗?
问题是我将i18n解析器bean的语言环境设置为cn
。更改它以zh
解决问题。问题是它将默认出现而没有任何错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句