1

사용자 정의 유효성 검사기에 로케일을 주입하고 내 applicationContext.xml에서 다음과 같은 구성을 가지고JSR-303 I 봄 3을 사용하고

<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.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을 갖는 작업을해야 그 때문에, 확실하지 않다 :

답변

4
주사

하지,하지만 정적 LocaleContextHolder 여기에 도움이 될 수 있습니다 벌써 :

<!-- 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" />