[英]JSR-303 inject locale into custom validator
我正在使用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
批注以验证邮政编码。 但是由于邮政编码在不同的国家/地区具有不同的格式,所以我很好奇是否可以将当前的语言环境放入验证器中。
不是通过注入,而是静态LocaleContextHolder可以在这里提供帮助:
LocaleContextHolder.setLocale(locale); // set locale for your request or based on user settings e.g. inside custom interceptor
和
LocaleContextHolder.getLocale(); // get locale inside your validator
但是我不确定为什么要显式地使用语言环境,因为拥有一个已经可以工作的LocaleChangeInterceptor
:
<!-- Declare the Interceptor -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="locale" />
</mvc:interceptors>
<!-- Declare the Resolver -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.