一、SimpleMappingExceptionResolver
如果希望对所有异常进行统一处理,可以使用 SimpleMappingExceptionResolver,它将异常类名映射为视图名,即发生异常时使用对应的视图报告异常
二、代码演示
a)Controller
@GetMapping("/hello")
public String hello() throws Exception {
// 抛出异常
throw new Exception("抛出异常");//Exception
}
@GetMapping("/test")
public String test() throws Exception {
// 模拟异常
int i = 5 / 0;
return "success";
}
/**
* <prop key="SQLException">sqlerror</prop>
*/
@GetMapping("/find")
public String find() throws Exception {
try {
// 模拟异常
int i = 5 / 0;
return "success";
} catch (Exception e) {
throw new SQLException("SQL出错查找数据失败!");
}
}
b)SpringMVC配置文件
<!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
<context:component-scan base-package="org.fkit.controller"/>
<!-- 默认配置方案 -->
<mvc:annotation-driven/>
<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>
<!-- 视图解析器 p:prefix属性表示前缀 p:suffix 表示后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/content/" p:suffix=".jsp"/>
<!-- 异常处理
p:defaultErrorView="error"表示所有没有指定的异常,都跳转到异常处理页面error
p:exceptionAttribute="ex"表示异常处理页面中访问的异常对象变量名是ex(默认exception)
-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"
p:defaultErrorView="error"
p:exceptionAttribute="ex">
<!-- 异常映射
exceptionMappings表示映射的异常,接受参数是一个Properties
key是异常类名,value是处理异常的页面
-->
<property name="exceptionMappings">
<props>
<prop key="IOException">ioerror</prop>
<prop key="SQLException">sqlerror</prop>
</props>
</property>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- exceptionMappings:配置哪些异常去哪些页面 -->
<property name="exceptionMappings">
<props>
<!-- key:异常全类名;value:要去的页面视图名; -->
<prop key="java.io.IOException">ioerror</prop>
<prop key="java.sql.SQLException">sqlerror</prop>
</props>
</property>
<!--指定错误信息取出时使用的key -->
<!--默认exception-->
<property name="exceptionAttribute" value="ex"/>
<property name="defaultErrorView" value="error"/>
</bean>
注: ioerror(WEB-INF/content/ioerror.jsp)、sqlerror( WEB-INF/content/sqlerror.jsp) 会被视图解析器解析。key 是异常类名或包名+类名
c)错误页面
WEB-INF/content/error.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>异常处理示例</title>
</head>
<body>
<h3>异常处理页面(error.jsp)</h3>
抛出异常信息:${requestScope.ex.message}
</body>
</html>
WEB-INF/content/sqlerror.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>异常处理示例</title>
</head>
<body>
<h3>特定异常处理页面(sqlerror.jsp)</h3>
抛出异常信息:${requestScope.ex.message}
</body>
</html>
WEB-INF/content/ioerror.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<style>
body {
background-color: red;
}
</style>
<head>
<title>IOException</title>
</head>
<body>
</body>
</html>
d)请求页面
index.jsp
<%@ page 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>异常处理示例</title>
</head>
<body>
<br>
<a href="hello">没有异常处理</a><br><br>
<a href="test">使用简单异常处理器处理异常</a><br><br>
<a href="find">使用简单异常处理器处理特定异常</a><br><br>
</body>
</html>
f)效果
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--版本锁定 后面好引用-->
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>