Example usage for org.springframework.web.bind ServletRequestDataBinder close

List of usage examples for org.springframework.web.bind ServletRequestDataBinder close

Introduction

In this page you can find the example usage for org.springframework.web.bind ServletRequestDataBinder close.

Prototype

public Map<?, ?> close() throws BindException 

Source Link

Document

Close this DataBinder, which may result in throwing a BindException if it encountered any errors.

Usage

From source file:org.nextframework.controller.MultiActionController.java

/**
 * Invokes the named method.//from ww w . j  av  a2s . c  o  m
 * <p>
 * Uses a custom exception handler if possible; otherwise, throw an
 * unchecked exception; wrap a checked exception or Throwable.
 * @param useCommand 
 */
@SuppressWarnings("deprecation")
protected final ModelAndView invokeNamedMethod(Method method, WebRequestContext request, Object useCommand)
        throws Exception {
    //TODO TRATAMENTO DE LOOP ETERNO (REFERENCIA CIRCULAR)

    do {
        Input input = null;
        boolean fromErrors = false;
        try {
            List<Object> params = new ArrayList<Object>(2);
            boolean hasRequestParameter = method.getParameterTypes().length > 0
                    && method.getParameterTypes()[0].isAssignableFrom(WebRequestContext.class)
                    && !method.getParameterTypes()[0].equals(Object.class);
            if (hasRequestParameter) {
                params.add(request);
            }

            if (useCommand == null) {
                input = getAnnotation(method, Input.class);//modificado em 22/10/2010, esse cdigo ficava dentro do if.. e s funcionava caso existissem commands
                if ((hasRequestParameter && method.getParameterTypes().length == 2)
                        || (!hasRequestParameter && method.getParameterTypes().length == 1)) {
                    Class<?> commandClass = getCommandClass(method, hasRequestParameter ? 1 : 0);
                    CommandInfo commandInfo = getCommandInfo(method);

                    Object command;
                    ServletRequestDataBinder binder;

                    if (!fromErrors) {
                        command = getCommandObject(request, commandClass, commandInfo);
                        binder = bind(request, command, commandInfo.validate);
                    } else {
                        command = getCommandObject(request, commandClass, commandInfo);
                        //se veio de erros nao fazer o bind novamente
                        binder = new ServletRequestDataBinder(command, getCommandName(command));
                    }

                    params.add(command);

                    if (binder.getBindingResult().hasErrors()) {
                        String inputAction = null;

                        if (input != null) {
                            inputAction = input.value();
                        } else {
                            logger.warn("No @Input specified for method " + method.getDeclaringClass().getName()
                                    + "." + method.getName() + ". Bind errors.");
                            new BindException(binder.getBindingResult()).printStackTrace();
                            if (commandInfo.session) {
                                //should reset the command
                                command = instantiateNewSessionCommand(request, commandClass,
                                        getSessionCommandName(commandClass, commandInfo));
                                inputAction = method.getName();
                            }
                        }
                        if (inputAction != null) {
                            ((DefaultWebRequestContext) request).setLastAction(inputAction);
                            Method handlerMethod = this.methodNameResolver.getHandlerMethod(inputAction);
                            ((DefaultWebRequestContext) request)
                                    .setBindException(new BindException(binder.getBindingResult()));
                            if (!handlerMethod.getName().equals(method.getName())) {
                                //o input deve ter o mesmo command do mtodo que declarou o input .. ento deixaremos o mtodo de input.. fazer o handling como o mesmo command
                                ((DefaultWebRequestContext) request)
                                        .setBindException(new BindException(binder.getBindingResult()));
                                method = handlerMethod;
                            }
                        } else {
                            binder.close();
                        }
                    }
                }
            } else {
                params.add(useCommand);
            }
            Object result = method.invoke(this.delegate, params.toArray(new Object[params.size()]));
            return convertActionResultToModelAndView(method, result);
        } catch (NoSuchRequestHandlingMethodException e) {
            throw e;
        } catch (NextException e) {
            throw e;
        } catch (InvocationTargetException ex) {
            // the invoked method threw exception
            if (input == null) {
                OnErrors onErrors = getAnnotation(method, OnErrors.class);
                if (onErrors != null) {
                    fromErrors = true;
                    ((DefaultWebRequestContext) request).setLastAction(onErrors.value());
                    Method methodErrors = this.methodNameResolver.getHandlerMethod(onErrors.value());
                    request.addError(ex.getTargetException());
                    logger.error("Erro ao invocar mtodo " + method.getName() + " da classe "
                            + this.getClass().getName() + ". Redirecionando para onErrors: " + onErrors.value(),
                            ex.getTargetException());
                    method = methodErrors;
                    continue;
                } else {
                    // nao tem input e no tem onerrors.. deixar a exceo vazar para algum handler se for o caso
                }
            } else {
                //se tem input.. redirecionar para input
                boolean sameMethod = false;
                String inputName = input.value();

                Method handlerMethod = this.methodNameResolver.getHandlerMethod(inputName);
                sameMethod = handlerMethod.getName().equals(method.getName());

                //   se for o mesmo mtodo.. deixar a excecao vazar (se mandar para o mtodo denovo vai dar loop eterno porque a excecao vai ocorrer novamente
                if (!sameMethod) {
                    // se nao for o mesmo mtodo.. redirecionar
                    // poderiamos mandar um flag j que o mtodo a ser invocado tem o mesmo command .. nesse caso economizariamos o bind
                    // mas vamos deixar fazer o bind novamente porque j pode ter ocorrido algum processamento que alterou os valores do command 
                    method = handlerMethod;
                    request.addError(ex.getTargetException());
                    ((DefaultWebRequestContext) request).setLastAction(inputName);
                    logger.error(
                            "Erro ao invocar mtodo " + method.getName() + " da classe "
                                    + this.getClass().getName() + ". Redirecionando para input: " + inputName,
                            ex.getTargetException());
                    continue;
                }
            }
            return handleException(request, ex.getTargetException());
        } catch (IllegalArgumentException ex) {
            throw new NextException(
                    "No foi possvel invocar o mtodo. Se estiver utilizando o mtodo continueToAction verifique se o mtodo que pede o redirecionamento e o mtodo de destino possuem a mesma classe de command",
                    ex);
        } catch (Exception ex) {
            // The binding process threw an exception.
            return handleException(request, ex);
        }
    } while (true);
}