Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 验证期间未使用Spring MessageSource_Java_Spring_Hibernate_Validation_Spring Mvc - Fatal编程技术网

Java 验证期间未使用Spring MessageSource

Java 验证期间未使用Spring MessageSource,java,spring,hibernate,validation,spring-mvc,Java,Spring,Hibernate,Validation,Spring Mvc,我无法在messages.properties中获取我的消息,这些消息将在我的表单备份对象的Spring验证期间使用 app-config.xml: <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages" /> </bean> 表单

我无法在messages.properties中获取我的消息,这些消息将在我的表单备份对象的Spring验证期间使用

app-config.xml:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basename" value="messages" />
</bean>
表单支持对象:

...
  @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注入控制器并输出以下内容,我会得到正确的消息: getMessage(“NotEmpty”,新对象[]{“password”},“默认空消息”,null)


那么为什么验证不使用my messages.properties呢?我正在运行Spring3.1.1。谢谢

@NotEmpty有一个可以设置的消息属性。因为您没有设置它,所以它使用默认消息

@NotEmpty(message="{NotEmpty}")

应该提供您所需的信息。

默认情况下,HibernateValidator将从
/WEB-INF/classes/ValidationMessages.properties获取其所有消息。它直接自己读取此文件,而不是通过Spring MessageSource


如果希望从Spring消息源进行翻译,请在上设置一个。这确实需要您在dispatcher servlet中手动设置验证bean,而不是依赖mvc:annotation-driven。

当您想要使用JSR 303验证为任何字段设置自定义消息时,您需要在属性文件中的消息键中指定bean名称(LoginForm)和字段名称(密码)。格式为约束\u NAME.COMMAND\u NAME.FIELD\u NAME

NotEmpty.loginform.password=This field should not be empty.

“Spring验证”只是一些接口。您是否将javax.validation与Hibernate Validator一起使用?我使用
@Valid
(javax.validation.Valid),并且在我的表单支持对象中使用类似
@NotEmpty
(org.Hibernate.Validator.constraints.NotEmpty)的验证。这就是你的意思吗?是的,如果我为每个@NotEmpty设置消息,它就会使用它。但是我想让它使用my messages.properties来获取NotEmpty的默认消息,这样我就不必为每个NotEmpty都设置它了。我从来没有见过你在春季谈论的内容。验证是字段绑定的,应按此方式进行处理。如果对具有相同问题的所有字段使用默认消息,则会降低反馈的价值。空密码与空用户名不同,并且给出诸如“You have a empty field”之类的一般响应实际上没有任何价值。我知道spring使用了我的messages.properties文件,方法是使用LocalValidatorFactoryBean并将验证器添加到我的mvc:annotation-driven标记中。你是对的,我不希望所有字段都有一个通用的NotEmpty消息。将message属性添加到NotEmpty确实有效,但我希望它能够在我的messages.properties中获取我的NotEmpty.password消息,而不显式地告诉它使用NotEmpty.password。目前我还不知道有这样的约定。该系统本身的设计非常灵活。如果您需要该功能,您可以自己添加它。您还可以将自定义验证bean注册为默认验证bean,并使用“”
NotEmpty.loginform.password=This field should not be empty.