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

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

Introduction

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

Prototype

public BindingResult getBindingResult() 

Source Link

Document

Return the BindingResult instance created by this DataBinder.

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.
 *//*from w w  w .  java2s. c  om*/
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();
}