在我的表单支持对象的 Spring 验证期间,我无法在 messages.properties 中获取我的消息。
应用程序配置.xml:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
WEB-INF/classes/messages.properties:
NotEmpty=This field should not be empty.
表单支持对象:
...
@NotEmpty
@Size(min=6, max=25)
private String password;
...
当我遍历 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]
如您所见,默认消息是“可能不是空的”,而不是我的消息“此字段不应为空”。
如果我将 messageSource 注入控制器并输出以下内容,我确实会收到正确的消息: messageSource.getMessage("NotEmpty", new Object [] {"password"}, "default empty message", null);
那么为什么不使用我的messages.properties 进行验证呢?我正在运行 Spring 3.1.1。谢谢!