Jer*_*emy 3 java validation spring hibernate spring-mvc
我无法在messages.properties中获取我的消息,以便在我的表单支持对象的Spring验证期间使用.
APP-config.xml文件:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
Run Code Online (Sandbox Code Playgroud)
WEB-INF /班/ messages.properties:
NotEmpty=This field should not be empty.
Run Code Online (Sandbox Code Playgroud)
表格支持对象:
...
@NotEmpty
@Size(min=6, max=25)
private String password;
...
Run Code Online (Sandbox Code Playgroud)
当我遍历BindingResult中的所有错误并输出ObjectError的toString时,我得到:
Field error in object 'settingsForm' on field 'password': rejected value []; codes [NotEmpty.settingsForm.password,NotEmpty.password,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [settingsForm.password,password]; arguments []; default message [password]]; default message [may not be empty]
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,默认消息是"可能不为空"而不是我的消息"此字段不应为空".
如果我将messageSource注入控制器并输出它,我会得到正确的消息:messageSource.getMessage("NotEmpty",new Object [] {"password"},"default empty message",null);
那么为什么不使用我的messages.properties进行验证呢?我正在运行Spring 3.1.1.谢谢!
默认情况下,HibernateValidator将从其获取所有消息./WEB-INF/classes/ValidationMessages.properties
它直接读取此文件,而不是通过Spring MessageSource.
如果要从Spring Message源进行翻译,请在LocalValidatorFactoryBean上设置一个.这确实需要您在调度程序servlet中手动设置验证bean,而不是依赖于mvc:annotation-driven.
归档时间: |
|
查看次数: |
10428 次 |
最近记录: |