基于配置的异常处理

SpringMVC提供了一个处理控制器,在方法执行过程中所出现的异常的接口:HandlerExceptionResolver,该接口的实现类有:DefaultHandlerExceptionResolver和SimpleMappingExceptionResolver

基于配置的异常处理

  1. 在配置中配置异常
<bean class="org.springframework.web.servlet.handler.simpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.ArithmeticException">error<prop>
        </props>
    </property>
    <property name="exceptionAttribute value="ex"></property>
<bean>

  

  1. 在前端设置响应连接:
基于thymeleaf:
<a th:href="@{/testException}">测试拦截</a>

  

  1. 创建error.html
在error.html中添加:
<p th:text="${ex}"></p>
  1. 在controller里面设置方法
@RequestMapping(/testException)
public String testException(){
  return "success";
}

基于注解的异常处理

image

posted on 2022-09-02 16:19  Kyrie  阅读(14)  评论(0)    收藏  举报