added SmartValidator interface with general support for validation hints; added custom @Valid annotation with support for JSR-303 validation groups; JSR-303 SpringValidatorAdapter and MVC data binding provide support for validation groups (SPR-6373)
This commit is contained in:
parent
49a2aaf023
commit
00133179df
|
|
@ -20,8 +20,10 @@ import java.lang.annotation.Annotation;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.validation.Errors;
|
import org.springframework.validation.Errors;
|
||||||
import org.springframework.web.bind.WebDataBinder;
|
import org.springframework.web.bind.WebDataBinder;
|
||||||
|
|
@ -100,14 +102,9 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
||||||
mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, request);
|
mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, request);
|
||||||
|
|
||||||
WebDataBinder binder = binderFactory.createBinder(request, target, name);
|
WebDataBinder binder = binderFactory.createBinder(request, target, name);
|
||||||
|
|
||||||
if (binder.getTarget() != null) {
|
if (binder.getTarget() != null) {
|
||||||
bindRequestParameters(binder, request);
|
bindRequestParameters(binder, request);
|
||||||
|
validateIfApplicable(binder, parameter);
|
||||||
if (isValidationApplicable(binder.getTarget(), parameter)) {
|
|
||||||
binder.validate();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (binder.getBindingResult().hasErrors()) {
|
if (binder.getBindingResult().hasErrors()) {
|
||||||
if (isBindExceptionRequired(binder, parameter)) {
|
if (isBindExceptionRequired(binder, parameter)) {
|
||||||
throw new BindException(binder.getBindingResult());
|
throw new BindException(binder.getBindingResult());
|
||||||
|
|
@ -116,7 +113,6 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
||||||
}
|
}
|
||||||
|
|
||||||
mavContainer.addAllAttributes(binder.getBindingResult().getModel());
|
mavContainer.addAllAttributes(binder.getBindingResult().getModel());
|
||||||
|
|
||||||
return binder.getTarget();
|
return binder.getTarget();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,10 +125,9 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
||||||
* @param request the current request
|
* @param request the current request
|
||||||
* @return the created model attribute, never {@code null}
|
* @return the created model attribute, never {@code null}
|
||||||
*/
|
*/
|
||||||
protected Object createAttribute(String attributeName,
|
protected Object createAttribute(String attributeName, MethodParameter parameter,
|
||||||
MethodParameter parameter,
|
WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception {
|
||||||
WebDataBinderFactory binderFactory,
|
|
||||||
NativeWebRequest request) throws Exception {
|
|
||||||
return BeanUtils.instantiateClass(parameter.getParameterType());
|
return BeanUtils.instantiateClass(parameter.getParameterType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -146,19 +141,19 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether to validate the model attribute.
|
* Validate the model attribute if applicable.
|
||||||
* The default implementation checks for {@code @javax.validation.Valid}.
|
* <p>The default implementation checks for {@code @javax.validation.Valid}.
|
||||||
* @param modelAttribute the model attribute
|
* @param binder the DataBinder to be used
|
||||||
* @param parameter the method argument
|
* @param parameter the method parameter
|
||||||
*/
|
*/
|
||||||
protected boolean isValidationApplicable(Object modelAttribute, MethodParameter parameter) {
|
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
|
||||||
Annotation[] annotations = parameter.getParameterAnnotations();
|
Annotation[] annotations = parameter.getParameterAnnotations();
|
||||||
for (Annotation annot : annotations) {
|
for (Annotation annot : annotations) {
|
||||||
if ("Valid".equals(annot.annotationType().getSimpleName())) {
|
if ("Valid".equals(annot.annotationType().getSimpleName())) {
|
||||||
return true;
|
Object hints = AnnotationUtils.getValue(annot);
|
||||||
|
binder.validate(hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue