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

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

Introduction

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

Prototype

@Nullable
public Validator getValidator() 

Source Link

Document

Return the primary Validator to apply after each binding step, if any.

Usage

From source file:org.opentides.web.processor.FormBindMethodProcessor.java

/**
 * Resolve the argument from the model or if not found instantiate it with
 * its default. The model attribute is then populated with request values 
 * via data binding and validated. If no validation error, the model
 * is retrieved from database and merged with the submitted form.
 *   //from  w w w.  jav  a2 s.co m
 * @throws BindException if data binding and validation result in an error
 * @throws Exception if WebDataBinder initialization fails.
 */
@SuppressWarnings("unchecked")
public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest nativeRequest, WebDataBinderFactory binderFactory) throws Exception {

    FormBind annot = parameter.getParameterAnnotation(FormBind.class);
    Class<?> clazz = parameter.getDeclaringClass();
    String name = getName(annot, parameter);
    HttpServletRequest request = (HttpServletRequest) nativeRequest.getNativeRequest();

    Method addForm = CacheUtil.getNewFormBindMethod(clazz);
    Object controller = beanFactory.getBean(parameter.getContainingClass());
    Object target = (addForm != null) ? addForm.invoke(controller, request)
            : BeanUtils.instantiateClass(parameter.getParameterType());
    MutablePropertyValues mpvs = new MutablePropertyValues(nativeRequest.getParameterMap());
    WebDataBinder binder = binderFactory.createBinder(nativeRequest, target, name);
    if (binder.getTarget() != null) {
        binder.bind(mpvs);
        if (binder.getValidator() != null)
            binder.validate();
        if (binder.getBindingResult().hasErrors()) {
            throw new BindException(binder.getBindingResult());
        }
        String method = request.getMethod().toLowerCase();

        // id should be the last segment of the uri
        String uri = request.getRequestURI();
        String sid = uri.substring(uri.lastIndexOf("/") + 1);
        Long id = StringUtil.convertToLong(sid, 0);

        // if target extends BaseEntity and for update, link target to database record
        if (("put".equals(method) || "post".equals(method)) && id > 0
                && BaseEntity.class.isAssignableFrom(parameter.getParameterType())) {
            // now retrieve record from database for updating
            Method updateForm = CacheUtil.getUpdateFormBindMethod(clazz);

            BaseEntity record = null;
            if (updateForm == null) {
                // no annotation, invoke from service
                Method getService = controller.getClass().getMethod("getService");
                if (getService == null) {
                    String message = "Cannot find method with @FormBind with update mode. "
                            + "Also, unable to find service associated to controller."
                            + "Please specify one that retrieves record from database.";
                    throw new InvalidImplementationException(message);
                }
                BaseCrudService<? extends BaseEntity> service = (BaseCrudService<? extends BaseEntity>) getService
                        .invoke(controller);
                record = (BaseEntity) service.load(sid);
            } else {
                // with annotation, invoke annotation
                record = (BaseEntity) updateForm.invoke(controller, sid, request);
            }

            if (record != null) {
                WebDataBinder updateBinder = binderFactory.createBinder(nativeRequest, record, name);
                updateBinder.bind(mpvs);
                mavContainer.addAllAttributes(updateBinder.getBindingResult().getModel());
                return updateBinder.getTarget();
            } else {
                String message = "Unable to find " + parameter.getParameterType().getSimpleName() + " with id="
                        + sid + " for update.";
                throw new DataAccessException(message);
            }
        } else if ("post".equals(method)) {
            mavContainer.addAllAttributes(binder.getBindingResult().getModel());
            return binder.getTarget();
        } else {
            throw new InvalidImplementationException(
                    "@FormBind argument annotation can only be used on POST or PUT methods.");
        }
    }
    mavContainer.addAllAttributes(binder.getBindingResult().getModel());
    return binder.getTarget();
}