java 验证期间未使用 Spring MessageSource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11128302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MessageSource not being used during validation
提问by Jeremy
I can't get my messages in messages.properties to be used during Spring validation of my form backing objects.
在我的表单支持对象的 Spring 验证期间,我无法在 messages.properties 中获取我的消息。
app-config.xml:
应用程序配置文件:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
WEB-INF/classes/messages.properties:
WEB-INF/classes/messages.properties:
NotEmpty=This field should not be empty.
Form Backing Object:
表单支持对象:
...
@NotEmpty
@Size(min=6, max=25)
private String password;
...
When I loop through all errors in the BindingResult and output the ObjectError's toString I get this:
当我遍历 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]
As you can see the default message is "may not be empty" instead of my message "This field should not be empty".
如您所见,默认消息是“可能不为空”,而不是我的消息“此字段不应为空”。
I do get my correct message if I inject the messageSource into a controller and output this: messageSource.getMessage("NotEmpty", new Object [] {"password"}, "default empty message", null);
如果我将 messageSource 注入控制器并输出,我确实得到了正确的消息: messageSource.getMessage("NotEmpty", new Object [] {"password"}, "default empty message", null);
So why isn't the validation using my messages.properties? I'm running Spring 3.1.1. Thanks!
那么为什么不使用我的messages.properties 进行验证?我正在运行 Spring 3.1.1。谢谢!
采纳答案by Joe
@NotEmpty has a message property you can set on it. Since you're not setting it, it uses the default message.
@NotEmpty 有一个你可以设置的消息属性。由于您没有设置它,它使用默认消息。
@NotEmpty(message="{NotEmpty}")
should give you what you are looking for.
应该给你你正在寻找的东西。
回答by Affe
By default HibernateValidator will get all of its messages from /WEB-INF/classes/ValidationMessages.properties
It reads this file directly itself, not through the Spring MessageSource.
默认情况下,HibernateValidator 将从/WEB-INF/classes/ValidationMessages.properties
它自己直接读取这个文件中获取它的所有消息,而不是通过 Spring MessageSource。
If you want translations from a Spring Message source, set one on the LocalValidatorFactoryBean. This does require you to manually set up a validation bean inside your dispatcher servlet, rather than rely on mvc:annotation-driven.
如果您想从 Spring Message 源进行翻译,请在LocalValidatorFactoryBean上设置一个。这确实需要您在调度程序 servlet 中手动设置验证 bean,而不是依赖 mvc:annotation-driven。
回答by Sunil Chavan
When you want to set the custom message for any field using JSR 303 validations then you need to specify the bean name(LoginForm) and field name(password) in key of the message in property file. The format is CONSTRAINT_NAME.COMMAND_NAME.FIELD_NAME
当您想使用 JSR 303 验证为任何字段设置自定义消息时,您需要在属性文件的消息键中指定 bean 名称(LoginForm)和字段名称(密码)。格式为 CONSTRAINT_NAME.COMMAND_NAME.FIELD_NAME
NotEmpty.loginform.password=This field should not be empty.