SpringMVC異常處理及攔截器

異常處理

jsp頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>異常處理</h3>

    <a href="ExceptionController/testException">整個異常</a>
</body>
</html>
ExceptionController
@Controller
@RequestMapping("/ExceptionController")
public class ExceptionController {

    @RequestMapping("/testException")
    public String testException() throws CustomException {
        try {
            //模擬異常
            int i = 10 / 0;
        } catch (Exception e) {
            e.printStackTrace();
            //拋出自定義異常
            throw new CustomException("系統維護中....");
        }
        return "exception";
    }
}
自定義異常類
public class CustomException extends Exception {
    private String message;

    public CustomException(String message) {
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
異常處理類
public class CustomExceptionResolver implements HandlerExceptionResolver {
    /**
     * 處理異常邏輯
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e                   當前拋出的異常對象
     * @return
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        CustomException exception = null;
        if (e instanceof CustomException) {
            exception = (CustomException) e;
        } else {
            exception = new CustomException("系統維護中...");
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("errMessage", exception);
        mv.setViewName("error");

        System.out.println(exception);
        return mv;
    }
}
在配置文件中配置異常處理器
<!--配置異常處理器-->
    <bean id="customExceptionResolver" class="com.sx.Exception.CustomExceptionResolver"></bean>

攔截器

jsp頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>攔截器</h3>

    <a href="InterceptorController/testInterceptor">測試</a>
</body>
</html>
跳轉頁面success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>訪問成功</h3>

<%System.out.println("success.jsp");%>

</body>
</html>
InterceptorController
@Controller
@RequestMapping("/InterceptorController")
public class InterceptorController {

    @RequestMapping("/testInterceptor")
    public String testInterceptor() {
        System.out.println("test執行");
        return "success";
    }
}
兩個攔截器
攔截器1
public class CustomInterceptor implements HandlerInterceptor {
    /**
     * 預處理,controller方法執行前執行
     *
     * @param request
     * @param response
     * @param handler
     * @return return true 表示放行,執行下一個攔截器,如果沒有就執行controller中的方法
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("preHandle執行了");
        return true;
    }

    /**
     * 後處理方法,controller方法執行後,success.jsp執行前執行
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle執行了");
    }


    /**
     * success.jsp執行後執行
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion執行了");
    }
}
攔截器2
public class CustomInterceptor2 implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("preHandle執行了2");
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle執行了2");
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion執行了2");
    }
}
配置攔截器
<!--配置攔截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--要攔截的具體方法-->
            <mvc:mapping path="/InterceptorController/*"/>
            <!--不攔截的方法-->
            <!--<mvc:exclude-mapping path=""/>-->
            <bean id="customInterceptor" class="com.sx.interceptor.CustomInterceptor"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean id="customInterceptor2" class="com.sx.interceptor.CustomInterceptor2"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
點擊超鏈接後Controller、攔截器、jsp的執行順序

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章