Example usage for org.springframework.web.bind WebDataBinder validate

List of usage examples for org.springframework.web.bind WebDataBinder validate

Introduction

In this page you can find the example usage for org.springframework.web.bind WebDataBinder validate.

Prototype

public void validate() 

Source Link

Document

Invoke the specified Validators, if any.

Usage

From source file:org.springframework.web.method.annotation.support.ModelAttributeMethodProcessor.java

/**
 * Resolve the argument from the model or if not found instantiate it with 
 * its default if it is available. The model attribute is then populated 
 * with request values via data binding and optionally validated
 * if {@code @java.validation.Valid} is present on the argument.
 * @throws BindException if data binding and validation result in an error
 * and the next method parameter is not of type {@link Errors}.
 * @throws Exception if WebDataBinder initialization fails.
 */// w  ww. j a  va 2  s . c o m
public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception {
    String name = ModelFactory.getNameForParameter(parameter);
    Object target = (mavContainer.containsAttribute(name)) ? mavContainer.getModel().get(name)
            : createAttribute(name, parameter, binderFactory, request);

    WebDataBinder binder = binderFactory.createBinder(request, target, name);

    if (binder.getTarget() != null) {
        bindRequestParameters(binder, request);

        if (isValidationApplicable(binder.getTarget(), parameter)) {
            binder.validate();
        }

        if (binder.getBindingResult().hasErrors()) {
            if (isBindExceptionRequired(binder, parameter)) {
                throw new BindException(binder.getBindingResult());
            }
        }
    }

    mavContainer.addAllAttributes(binder.getBindingResult().getModel());

    return binder.getTarget();
}