SpringMVC异常处理及拦截器

本文详细介绍了Spring MVC框架中的异常处理机制和拦截器功能。通过实例演示了如何自定义异常类、创建异常处理控制器及配置异常处理器,实现统一的异常捕获与响应。同时,深入探讨了拦截器的实现方式,包括预处理、后处理和完成后的处理方法,以及如何在Spring MVC中配置多个拦截器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常处理
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的执行顺序

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值