Example usage for org.springframework.web.util WebUtils exposeErrorRequestAttributes

List of usage examples for org.springframework.web.util WebUtils exposeErrorRequestAttributes

Introduction

In this page you can find the example usage for org.springframework.web.util WebUtils exposeErrorRequestAttributes.

Prototype

public static void exposeErrorRequestAttributes(HttpServletRequest request, Throwable ex,
        @Nullable String servletName) 

Source Link

Document

Expose the Servlet spec's error attributes as javax.servlet.http.HttpServletRequest attributes under the keys defined in the Servlet 2.3 specification, for error pages that are rendered directly rather than through the Servlet container's error page resolution: javax.servlet.error.status_code , javax.servlet.error.exception_type , javax.servlet.error.message , javax.servlet.error.exception , javax.servlet.error.request_uri , javax.servlet.error.servlet_name .

Usage

From source file:com.sinosoft.one.mvc.web.impl.thread.ModuleEngine.java

public Object execute(Mvc mvc) throws Throwable {
    Invocation inv = mvc.getInvocation();

    // Spring?applicationContextrequest,messageSource/
    inv.getRequest().setAttribute(MvcConstants.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            module.getApplicationContext());

    boolean isMultiPartRequest = false;
    try {//w  w  w  .  j av  a2s  . com
        isMultiPartRequest = checkMultipart(inv);
        return mvc.doNext();
    } catch (Throwable invException) {
        // (?)?ControllerErrorHanlder?

        Throwable cause = invException;
        // ???InvocationTargetException??
        while (cause instanceof InvocationTargetException) {
            cause = ((InvocationTargetException) cause).getTargetException();
        }
        // 
        Module errorHandlerModule = module;
        ControllerErrorHandler errorHandler = errorHandlerModule.getErrorHandler();
        while (errorHandler == null && errorHandlerModule != null) {
            errorHandlerModule = errorHandlerModule.getParent();
            if (errorHandlerModule != null) {
                errorHandler = errorHandlerModule.getErrorHandler();
                if (errorHandler != null) {
                    if (errorHandler.getClass().isAnnotationPresent(NotForSubModules.class)) {
                        errorHandler = null;
                        continue;
                    }
                }
            } else {
                errorHandler = null;
                break;
            }
        }
        Object instruction = null;
        if (errorHandler != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("exception happended " + errorHandler.getClass().getName()
                        + " will handle the exception: " //
                        + cause.getClass().getName() + ":" + cause.getMessage());
            }
            mvc.getInvocation().setViewModule(errorHandlerModule);
            // 
            HttpServletRequest request = mvc.getInvocation().getRequest();
            WebUtils.exposeErrorRequestAttributes(request, cause, null);
            StackTraceSimplifier.simplify(cause); //
            instruction = errorHandler.onError(mvc.getInvocation(), cause);
        }

        // onErrornull??throw
        // rethrow?causeinvException????
        if ((errorHandler == null) || (instruction == null)) {
            if (invException instanceof Exception) {
                throw (Exception) invException;
            } else {
                throw (Error) invException;
            }
        }
        return instruction;
    } finally {
        if (isMultiPartRequest) {
            cleanupMultipart(inv);
        }
    }
}

From source file:net.paoding.rose.web.impl.thread.ModuleEngine.java

@Override
public Object execute(Rose rose) throws Throwable {
    Invocation inv = rose.getInvocation();

    // Spring?applicationContextrequest,messageSource/
    inv.getRequest().setAttribute(RoseConstants.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            module.getApplicationContext());

    boolean isMultiPartRequest = false;
    try {//from w  ww .j av a2s. c om
        isMultiPartRequest = checkMultipart(inv);
        return rose.doNext();
    } catch (Throwable invException) {
        // (?)?ControllerErrorHanlder?

        Throwable cause = invException;
        // ???InvocationTargetException??
        while (cause instanceof InvocationTargetException) {
            cause = ((InvocationTargetException) cause).getTargetException();
        }
        // 
        Module errorHandlerModule = module;
        ControllerErrorHandler errorHandler = errorHandlerModule.getErrorHandler();
        while (errorHandler == null && errorHandlerModule != null) {
            errorHandlerModule = errorHandlerModule.getParent();
            if (errorHandlerModule != null) {
                errorHandler = errorHandlerModule.getErrorHandler();
                if (errorHandler != null) {
                    if (errorHandler.getClass().isAnnotationPresent(NotForSubModules.class)) {
                        errorHandler = null;
                        continue;
                    }
                }
            } else {
                errorHandler = null;
                break;
            }
        }
        Object instruction = null;
        if (errorHandler != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("exception happended " + errorHandler.getClass().getName()
                        + " will handle the exception: " //
                        + cause.getClass().getName() + ":" + cause.getMessage());
            }
            rose.getInvocation().setViewModule(errorHandlerModule);
            // 
            HttpServletRequest request = rose.getInvocation().getRequest();
            WebUtils.exposeErrorRequestAttributes(request, cause, null);
            StackTraceSimplifier.simplify(cause); //
            instruction = errorHandler.onError(rose.getInvocation(), cause);
        }

        // onErrornull??throw
        // rethrow?causeinvException????
        if ((errorHandler == null) || (instruction == null)) {
            if (invException instanceof Exception) {
                throw (Exception) invException;
            } else {
                throw (Error) invException;
            }
        }
        return instruction;
    } finally {
        if (isMultiPartRequest) {
            cleanupMultipart(inv);
        }
    }
}

From source file:com.laxser.blitz.web.impl.thread.ModuleEngine.java

@Override
public Object execute(Blitz rose) throws Throwable {
    Invocation inv = rose.getInvocation();

    // Spring?applicationContextrequest,messageSource/
    inv.getRequest().setAttribute(BlitzConstants.WEB_APPLICATION_CONTEXT_ATTRIBUTE,
            module.getApplicationContext());

    boolean isMultiPartRequest = false;
    try {/*from  w  w w .j a  v a2  s. c o m*/
        isMultiPartRequest = checkMultipart(inv);
        return rose.doNext();
    } catch (Throwable invException) {
        // (?)?ControllerErrorHanlder?

        Throwable cause = invException;
        // ???InvocationTargetException??
        while (cause instanceof InvocationTargetException) {
            cause = ((InvocationTargetException) cause).getTargetException();
        }
        // 
        Module errorHandlerModule = module;
        ControllerErrorHandler errorHandler = errorHandlerModule.getErrorHandler();
        while (errorHandler == null && errorHandlerModule != null) {
            errorHandlerModule = errorHandlerModule.getParent();
            if (errorHandlerModule != null) {
                errorHandler = errorHandlerModule.getErrorHandler();
                if (errorHandler != null) {
                    if (errorHandler.getClass().isAnnotationPresent(NotForSubModules.class)) {
                        errorHandler = null;
                        continue;
                    }
                }
            } else {
                errorHandler = null;
                break;
            }
        }
        Object instruction = null;
        if (errorHandler != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("exception happended " + errorHandler.getClass().getName()
                        + " will handle the exception: " //
                        + cause.getClass().getName() + ":" + cause.getMessage());
            }
            rose.getInvocation().setViewModule(errorHandlerModule);
            // 
            HttpServletRequest request = rose.getInvocation().getRequest();
            WebUtils.exposeErrorRequestAttributes(request, cause, null);
            StackTraceSimplifier.simplify(cause); //
            instruction = errorHandler.onError(rose.getInvocation(), cause);
        }

        // onErrornull??throw
        // rethrow?causeinvException????
        if ((errorHandler == null) || (instruction == null)) {
            if (invException instanceof Exception) {
                throw (Exception) invException;
            } else {
                throw (Error) invException;
            }
        }
        return instruction;
    } finally {
        if (isMultiPartRequest) {
            cleanupMultipart(inv);
        }
    }
}

From source file:com.mystudy.source.spring.mvc.DispatcherServlet.java

/**
 * Determine an error ModelAndView via the registered HandlerExceptionResolvers.
 * @param request current HTTP request/*from   w w  w  .  jav  a  2 s.  c  o  m*/
 * @param response current HTTP response
 * @param handler the executed handler, or {@code null} if none chosen at the time of the exception
 * (for example, if multipart resolution failed)
 * @param ex the exception that got thrown during handler execution
 * @return a corresponding ModelAndView to forward to
 * @throws Exception if no error ModelAndView found
 */
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
        Object handler, Exception ex) throws Exception {

    // Check registered HandlerExceptionResolvers...
    ModelAndView exMv = null;
    for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
        exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
        if (exMv != null) {
            break;
        }
    }
    if (exMv != null) {
        if (exMv.isEmpty()) {
            return null;
        }
        // We might still need view name translation for a plain error model...
        if (!exMv.hasView()) {
            exMv.setViewName(getDefaultViewName(request));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Handler execution resulted in exception - forwarding to resolved error view: " + exMv,
                    ex);
        }
        WebUtils.exposeErrorRequestAttributes(request, ex, getServletName());
        return exMv;
    }

    throw ex;
}

From source file:org.springframework.web.servlet.DispatcherServlet.java

/**
 * Determine an error ModelAndView via the registered HandlerExceptionResolvers.
 * @param request current HTTP request/*from  w w  w .ja v  a 2  s  .c o m*/
 * @param response current HTTP response
 * @param handler the executed handler, or {@code null} if none chosen at the time of the exception
 * (for example, if multipart resolution failed)
 * @param ex the exception that got thrown during handler execution
 * @return a corresponding ModelAndView to forward to
 * @throws Exception if no error ModelAndView found
 */
@Nullable
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
        @Nullable Object handler, Exception ex) throws Exception {

    // Check registered HandlerExceptionResolvers...
    ModelAndView exMv = null;
    if (this.handlerExceptionResolvers != null) {
        for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
            exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
            if (exMv != null) {
                break;
            }
        }
    }
    if (exMv != null) {
        if (exMv.isEmpty()) {
            request.setAttribute(EXCEPTION_ATTRIBUTE, ex);
            return null;
        }
        // We might still need view name translation for a plain error model...
        if (!exMv.hasView()) {
            String defaultViewName = getDefaultViewName(request);
            if (defaultViewName != null) {
                exMv.setViewName(defaultViewName);
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Handler execution resulted in exception - forwarding to resolved error view: " + exMv,
                    ex);
        }
        WebUtils.exposeErrorRequestAttributes(request, ex, getServletName());
        return exMv;
    }

    throw ex;
}

From source file:org.springframework.web.servlet.DispatcherServletMod.java

/**
 * Determine an error ModelAndView via the registered
 * HandlerExceptionResolvers./*from   w w  w  . java2s. c o m*/
 * 
 * @param request
 *            current HTTP request
 * @param response
 *            current HTTP response
 * @param handler
 *            the executed handler, or {@code null} if none chosen at the
 *            time of the exception (for example, if multipart resolution
 *            failed)
 * @param ex
 *            the exception that got thrown during handler execution
 * @return a corresponding ModelAndView to forward to
 * @throws Exception
 *             if no error ModelAndView found
 */
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
        Object handler, Exception ex) throws Exception {

    // Check registered HandlerExceptionResolvers...
    ModelAndView exMv = null;
    for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
        exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
        if (exMv != null) {
            break;
        }
    }
    if (exMv != null) {
        if (exMv.isEmpty()) {
            return null;
        }
        // We might still need view name translation for a plain error
        // model...
        if (!exMv.hasView()) {
            exMv.setViewName(getDefaultViewName(request));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Handler execution resulted in exception - forwarding to resolved error view: " + exMv,
                    ex);
        }
        WebUtils.exposeErrorRequestAttributes(request, ex, getServletName());
        return exMv;
    }

    throw ex;
}

From source file:org.springframework.web.servlet.LogDispatcherServlet.java

/**
 * Determine an error ModelAndView via the registered HandlerExceptionResolvers.
 * @param request current HTTP request//from  www. ja  v a2  s .  co m
 * @param response current HTTP response
 * @param handler the executed handler, or {@code null} if none chosen at the time of the exception
 * (for example, if multipart resolution failed)
 * @param ex the exception that got thrown during handler execution
 * @return a corresponding ModelAndView to forward to
 * @throws Exception if no error ModelAndView found
 */
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
        Object handler, Exception ex) throws Exception {

    // Check registered HandlerExceptionResolvers...
    ModelAndView exMv = null;
    for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
        exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
        if (exMv != null) {
            break;
        }
    }
    if (exMv != null) {
        if (exMv.isEmpty()) {
            request.setAttribute(EXCEPTION_ATTRIBUTE, ex);
            return null;
        }
        // We might still need view name translation for a plain error model...
        if (!exMv.hasView()) {
            exMv.setViewName(getDefaultViewName(request));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Handler execution resulted in exception - forwarding to resolved error view: " + exMv,
                    ex);
        }
        WebUtils.exposeErrorRequestAttributes(request, ex, getServletName());
        return exMv;
    }

    throw ex;
}

From source file:org.springframework.web.servlet.SimpleDispatcherServlet.java

/**
 * Determine an error ModelAndView via the registered HandlerExceptionResolvers.
 * @param request current HTTP request/*from   w w  w .j av  a  2s  .com*/
 * @param response current HTTP response
 * @param handler the executed handler, or <code>null</code> if none chosen at the time of the exception
 * (for example, if multipart resolution failed)
 * @param ex the exception that got thrown during handler execution
 * @return a corresponding ModelAndView to forward to
 * @throws Exception if no error ModelAndView found
 */
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
        Object handler, Exception ex) throws Exception {

    // Check registerer HandlerExceptionResolvers...
    ModelAndView exMv = null;
    for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
        exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
        if (exMv != null) {
            break;
        }
    }
    if (exMv != null) {
        if (exMv.isEmpty()) {
            return null;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Handler execution resulted in exception - forwarding to resolved error view: " + exMv,
                    ex);
        }
        WebUtils.exposeErrorRequestAttributes(request, ex, getServletName());
        return exMv;
    }

    throw ex;
}