Rom*_*n T 5 spring hibernate-validator
我使用Spring 3.1.1和Hibernate-validator 4.3.0.Final并且在更改默认的MessageInterpolator时遇到问题,它从ValidationMessages(在类路径中)获取验证消息.
我想使用ResourceBundleMessageInterpolator,它将从我的spring messageSource获取消息
我在application-context.xml中做了类似的事情:
<bean id="resourceBundleMessageInterpolator"
class="org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator">
<constructor-arg index="0">
<bean class="org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator">
<constructor-arg index="0" ref="messageSource"/>
</bean>
</constructor-arg>
</bean>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="messageInterpolator" ref="resourceBundleMessageInterpolator"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
当我在日志中启动我的Web应用程序时,我看到:
11:04:07,402 DEBUG [org.hibernate.validator.internal.engine.ConfigurationImpl] -
Setting custom MessageInterpolator of type
org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator
11:04:07,402 DEBUG [org.hibernate.validator.internal.engine.ConfigurationImpl] -
Setting custom ConstraintValidatorFactory of type org.springframework .validation.beanvalidation.SpringConstraintValidatorFactory
Run Code Online (Sandbox Code Playgroud)
所以你可以看到,它不是我想要的ResourceBundleMessageInterpolator.它是LocaleContextMessageInterpolator
稍后,当我尝试验证某些内容时,我只是从ValidationMessages.properties获取消息,而不是来自spring消息源:
11:08:09,397 DEBUG [org.hibernate.validator.resourceloading.PlatformResourceBundleLocator] -
ValidationMessages not found.
11:08:09,413 DEBUG [org.hibernate.validator.resourceloading.PlatformResourceBundleLocator] -
org.hibernate.validator.ValidationMessages found.
Run Code Online (Sandbox Code Playgroud)
从application-context.xml可以看出我想使用MessageSourceResourceBundleLocator,但是它已经使用了,我不知道为什么要使用PlatformResourceBundleLocator
有任何想法吗?
Xae*_*ess 15
您不必自己声明ResourceBundleMessageInterpolator和MessageSourceResourceBundleLocatorbean(除非必须),它们是LocalValidatorFactoryBean在您提供时创建的messageSource:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource" ref="messageSource"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
(它在引擎盖下这样做:)
public void setValidationMessageSource(MessageSource messageSource) {
this.messageInterpolator = HibernateValidatorDelegate.buildMessageInterpolator(messageSource);
}
// (...)
private static class HibernateValidatorDelegate {
public static MessageInterpolator buildMessageInterpolator(MessageSource messageSource) {
return new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(messageSource));
}
}
Run Code Online (Sandbox Code Playgroud)
因此,通过简化的bean定义,您是否获得相同的调试输出?你在哪里使用"验证器"参考?您可能必须使用<mvc:annotation-driven validator="validator">例如.
| 归档时间: |
|
| 查看次数: |
9796 次 |
| 最近记录: |