2012-06-27 11 views
5

Używam Spring 3.1.1 i Hibernate-validator 4.3.0.Final i mam problem ze zmianą domyślnego MessageInterpolator, który pobiera wiadomości walidacji z ValidationMessages (w ścieżce klas).MessageInterpolator na wiosnę

chcę używać ResourceBundleMessageInterpolator która odbędzie wiadomości z mojego wiosennego MessageSource

zrobiłem coś takiego w mojej aplikacji 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> 

A kiedy zaczynam moją aplikację internetową w postaci kłód I zobacz:

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 

Tak, jak chcę, nie jest to ResourceBundleMessageInterpolator. To LocaleContextMessageInterpolator

Później, gdy staram się potwierdzić coś, co po prostu wiadomości z ValidationMessages.properties, a nie z komunikatów wiosennej źródła:

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. 

Jak widać z aplikacji context.xml chcę use MessageSourceResourceBundleLocator , ale jest używany, nie wiem dlaczego PlatformResourceBundleLocator

Jakieś pomysły?

Odpowiedz

15

Nie musisz zadeklarować ResourceBundleMessageInterpolator i MessageSourceResourceBundleLocator fasolę przez siebie (chyba że trzeba), są one tworzone przez LocalValidatorFactoryBean gdy podasz messageSource:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> 
    <property name="validationMessageSource" ref="messageSource"/> 
</bean> 

(Czyni to pod maską :)

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)); 
    } 
} 

Czy w przypadku uproszczonej definicji fasoli otrzymujesz takie same dane wyjściowe debugowania? Gdzie korzystasz z "validatora"? Być może będziesz musiał użyć na przykład <mvc:annotation-driven validator="validator">.

+4

Wielkie dzięki za odpowiedź. Wcześniej korzystałem z prostszej wersji z setValidationMessageSource, jak napisałeś. Zachowanie było takie, jak opisałem na początku. Chodzi o to, że przegapiłem walidator atrybutów = "validator" w moim . Dodałem atrybut i to działa. –

+0

co zrobić, jeśli korzystam tylko z modułu Spring-Web, a nie Spring-Webmvc? –

+0

gdzie użyć weryfikatora poprawnego w konfiguracji opartej na adnotacji – karthi