基于配置的异常处理
SpringMVC提供了一个处理控制器,在方法执行过程中所出现的异常的接口:HandlerExceptionResolver,该接口的实现类有:DefaultHandlerExceptionResolver和SimpleMappingExceptionResolver
基于配置的异常处理
- 在配置中配置异常
<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>
- 在前端设置响应连接:
基于thymeleaf:
<a th:href="@{/testException}">测试拦截</a>
- 创建error.html
在error.html中添加:
<p th:text="${ex}"></p>
- 在controller里面设置方法
@RequestMapping(/testException)
public String testException(){
return "success";
}
基于注解的异常处理

浙公网安备 33010602011771号