JSR-303 将语言环境注入自定义验证器
我正在使用 Spring 3,并且在 applicationContext.xml 中有以下配置:
<bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="classpath:messages/validation_messages" p:defaultEncoding="UTF-8" p:cacheSeconds="3" />
<bean id="globalValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="validationMessageSource" />
</property>
</bean>
一切都与语言环境等一起正常工作
。但是,我想知道是否可以将语言环境注入到我构建的自定义验证器中。我创建了一个 @CheckZip
注释来验证邮政编码。但由于邮政编码在不同国家有不同的格式,我很好奇是否可以将当前区域设置放入验证器中。
I am using Spring 3 and I have the following configuration in my applicationContext.xml:
<bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="classpath:messages/validation_messages" p:defaultEncoding="UTF-8" p:cacheSeconds="3" />
<bean id="globalValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="validationMessageSource" />
</property>
</bean>
Everything works fine with locales etc.
However, I was wondering if it is possible to inject the locale to a custom validator I have constructed. I have created a @CheckZip
annotation for validating Zip codes. But since zip codes have different formats in different countries I am curious whether I could throw the current locale into the validator.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是通过注入,而是静态 LocaleContextHolder 可以在这里提供帮助:
但
我不确定为什么你需要明确的区域设置,因为有一个应该已经可以工作的
LocaleChangeInterceptor
:Not by injection, but static LocaleContextHolder can help here:
and
But I'm not sure why you need the locale explicitly, because having a
LocaleChangeInterceptor
that should work already: