Dear Spring Community,
I am trying to implement the following:
- I would like to have my own validator for each controller ( via
@InitBinder ) - I would like Spring to call
validator.validate()(therefore not this way ) - I would like to use the JSR-303 annotation
@Validfor this - The bean for validation (
RegistrationForm) does not contain JSR-303 annotations for each field. - I do not want to include a validation implementation (such as Hibernate ) in the classpath; it will be useless as stated above.
I basically follow the steps below here :
- I add
javax.validation.validation-api:validation-apias my addiction - I use
<mvc:annotation-driven /> - I mark my model with
@Valid:
public String onRegistrationFormSubmitted(@ModelAttribute("registrationForm") @Valid RegistrationForm registrationForm, BindingResult result) ...
So what happens is that the validation API tries to find any implementation and fails:
Caused by: javax.validation.ValidationException: Unable to find a default provider
at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264)
at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:183)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
The conclusion is to define a property validatorfor AnnotationDrivenBeanDefinitionParser:
<bean name="validator" class="org.company.module.RegistrationFormValidator" />
<mvc:annotation-driven validator="validator" />
but this approach means that the validator will be installed for all controllers on ConfigurableWebBindingInitializer.initBinder().
I understand that I am trying to use the framework in a special way, but what the community will say if there is a special value for the property validatorthat says that the validator does not need permission, for example
<mvc:annotation-driven validator="manual" />
with special processing:
@@ -152,6 +152,10 @@
private RuntimeBeanReference getValidator(Element element, Object source, ParserContext parserContext) {
if (element.hasAttribute("validator")) {
+ if ("manual".equals(element.getAttribute("validator"))) {
+ return null;
+ }
+
return new RuntimeBeanReference(element.getAttribute("validator"));
}
else if (jsr303Present) {
Any feedback is appreciated.
PS Repost from the Spring Forum .
dma_k source
share