SpringMVC统一异常处理(七)

来源:互联网 发布:多益网络账号登陆 编辑:程序博客网 时间:2024/05/16 14:37

GitHub地址:

springMVC:

https://github.com/asd821300801/springMVC.git

Maven管理的springMVC:

https://github.com/asd821300801/MVC.git


1、application-mvc.xml 配置文件中加入:

<!-- 异常处理 -->    <bean id="exceptionResolver"        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">        <property name="defaultErrorView">            <value>/error/500</value>        </property>        <property name="defaultStatusCode">            <value>500</value>        </property>        <!-- spring就会使用apache的org.apache.commons.logging.Log日志工具, 记录这个异常,级别是warn -->        <property name="warnLogCategory">            <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver            </value>        </property>    </bean>

1


2、在pages/目录下创建error/500.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>    系统错误:${exception }</body></html>

2


3、测试:

在controller层模拟一个异常

int i = 1/0;//模拟一个不能被0除的异常


访问地址:

http://localhost:8080/springmvc/user/viewRest

3

0 0
原创粉丝点击