SpringMVC(11) -- 异常处理器

SpringMVC学习笔记

源码地址

十一、异常处理器

11.1)基于配置的异常处理

SpringMVC提供了一个处理控制器方法执行过程中所出现的异常的接口:HandlerExceptionResolver

HandlerExceptionResolver接口的实现类有:DefaultHandlerExceptionResolver和SimpleMappingExceptionResolver

SpringMVC提供了自定义的异常处理器SimpleMappingExceptionResolver,修改配置文件SpringMVC.xml,代码如下:

    <!--配置异常处理-->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <!-- properties的键表示处理器方法执行过程中出现的异常
                     properties的值表示若出现指定异常时,设置一个新的视图名称,跳转到指定页面
                 -->
                <prop key="java.lang.ArithmeticException">error</prop>
            </props>
        </property>
        <!--exceptionAttribute属性设置一个属性名,将出现的异常信息在请求域中进行共享-->
        <property name="exceptionAttribute" value="ex"></property>
    </bean>

修改 TestController.java ,代码如下:

 // 异常处理器
    @RequestMapping("/testExceptionHandler")
    public String testExceptionHandler() {
        System.out.println(1 / 0);
        return "success";
    }

新建错误页面 error.html ,代码如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
出现错误
<p th:text="${ex}"></p>
</body>
</html>

修改首页 index.html ,代码如下:

<a th:href="@{/testExceptionHandler}">测试异常处理</a><br>

测试:配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo5/,点击“测试异常处理”超链接,页面跳转到成功页:http://localhost:8080/SpringMvcDemo5/testExceptionHandler,页面如下:

 控制台输出:

17:11:47.708 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/SpringMvcDemo5/testExceptionHandler", parameters={}
17:11:47.721 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.study.mvc.controller.TestController#testExceptionHandler()
FirstInterceptor-->preHandle
SecondInterceptor-->preHandle
17:11:47.759 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving to view 'error' based on mapping [java.lang.ArithmeticException]
17:11:47.759 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolved [java.lang.ArithmeticException: / by zero] to ModelAndView [view="error"; model={ex=java.lang.ArithmeticException: / by zero}]
17:11:47.759 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.DispatcherServlet - Using resolved error view: ModelAndView [view="error"; model={ex=java.lang.ArithmeticException: / by zero}]
SecondInterceptor-->afterCompletion
FirstInterceptor-->afterCompletion
17:11:47.852 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK

11.2)基于注解的异常处理

删除上述配置,使用注解进行异常处理,新建 ExceptionController.java,代码如下:

//@ControllerAdvice:将当前类标识为异常处理的组件
@ControllerAdvice
public class ExceptionController {
​
    @ExceptionHandler(value = {ArithmeticException.class, NullPointerException.class})
    public String testException(Exception ex, Model model) {
        model.addAttribute("ex", ex);
        return "error";
    }
}

测试:配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo5/,点击“测试异常处理”超链接,页面跳转到成功页:http://localhost:8080/SpringMvcDemo5/testExceptionHandler,页面如下:

 控制台输出:

17:15:33.560 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/SpringMvcDemo5/testExceptionHandler", parameters={}
17:15:33.566 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.study.mvc.controller.TestController#testExceptionHandler()
FirstInterceptor-->preHandle
SecondInterceptor-->preHandle
17:15:33.592 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Using @ExceptionHandler com.study.mvc.controller.ExceptionController#testException(Exception, Model)
17:15:33.600 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolved [java.lang.ArithmeticException: / by zero] to ModelAndView [view="error"; model={ex=java.lang.ArithmeticException: / by zero}]
17:15:33.600 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - Using resolved error view: ModelAndView [view="error"; model={ex=java.lang.ArithmeticException: / by zero}]
SecondInterceptor-->afterCompletion
FirstInterceptor-->afterCompletion
17:15:33.669 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值