介绍&步骤
视频教程: https://www.bilibili.com/video/BV1WZ4y1P7Bp?p=116
官方笔记链接:https://pan.baidu.com/s/1dnL5hwOPHPMNgb81yzQIOQ
提取码:2022
异常处理的思路
异常处理两种方式
- 使用Spring MVC提供的简单异常处理器
SimpleMappingExceptionResolver
- 实现Spring的异常处理接口
HandlerExceptionResolver
自定义自己的异常处理器
1. 前期环境准备
项目目录结构
1.1 导入坐标
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
1.2 核心测试代码
1.2.1 MyException 异常类
public class MyException extends Exception{
}
1.2.2 DemoService 接口类
public interface DemoService {
void show1();
void show2();
void show3() throws FileNotFoundException;
void show4();
void show5() throws MyException;
}
1.2.3 DemoServiceImpl 实现类
public class DemoServiceImpl implements DemoService{
public void show1() {
System.out.println("抛出类型转换异常....");
Object str = "zhangsan";
Integer num = (Integer)str;
}
public void show2() {
System.out.println("抛出除零异常....");
int i = 1/0;
}
public void show3() throws FileNotFoundException {
System.out.println("文件找不到异常....");
InputStream in = new FileInputStream("C:/xxx/xxx/xxx.txt");
}
public void show4() {
System.out.println("空指针异常.....");
String str = null;
str.length();
}
public void show5() throws MyException {
System.out.println("自定义异常....");
throw new MyException();
}
}
1.2.4 Controller层测试类
@Controller // 让前端控制器检测到
public class DemoController {
@Autowired // 自动注入
private DemoService demoService;
@RequestMapping(value = "/show")
public String show() throws FileNotFoundException, MyException {
System.out.println("show running......");
// demoService.show1();
//demoService.show2();
//demoService.show3();
//demoService.show4();
demoService.show5();
return "index";
}
}
1.3 文件配置
1.3.1 applicationContext.xml 文件配置
<!-- 配置bean 因为DemoController要用到注入 容器里面没有的话会报错 -->
<bean id="demoService" class="review.service.DemoServiceImpl"></bean>
1.3.2 spring-mvc.xml 文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--1、mvc注解驱动-->
<mvc:annotation-driven/>
<!--2、配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--3、静态资源权限开放-->
<mvc:default-servlet-handler/>
<!--4、组件扫描 扫描Controller-->
<context:component-scan base-package="review.controller"/>
</beans>
1.3.3 web.xml 文件配置
<!--全局参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--监听器 加载applicationContext.xml文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--前端控制器 加载spring-mvc.xml文件-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
1.4 页面准备
error.jsp
页面
<h1>通用的错误提示页面${info}</h1>
error1.jsp
页面
<h1>error1 类型转化异常</h1>
error2.jsp
页面
<h1>error2 自定义异常</h1>
index.jsp
页面
<h2>Hello World!</h2>
2. 简单异常处理器SimpleMappingExceptionResolver
直接在spring-mvc.xml
中配置下面的异常处理器即可
<!--简单异常处理器-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"/>
<property name="exceptionMappings">
<map>
<entry key="java.lang.ClassCastException" value="error1"/>
<entry key="review.exception.MyException" value="error2"/>
</map>
</property>
</bean>
3. 自定义异常处理步骤
① 创建异常处理器类实现HandlerExceptionResolver
② 配置异常处理器
③ 编写异常页面
④ 测试异常跳转
3.1 创建异常处理器类
//自定义异常处理步骤
public class MyExceptionResolver implements HandlerExceptionResolver {
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView modelAndView = new ModelAndView();
if(e instanceof MyException){
modelAndView.addObject("info", "自定义异常");
}else if(e instanceof ClassCastException){
modelAndView.addObject("info", "类转化异常");
}
modelAndView.setViewName("error");
return modelAndView;
}
}
3.2 配置异常处理器
在spring.xml
中配置自定义的异常处理器
将之前的简单异常处理器注释掉 不然会覆盖
注释
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"/>
<property name="exceptionMappings">
<map>
<entry key="java.lang.ClassCastException" value="error1"/>
<entry key="review.exception.MyException" value="error2"/>
</map>
</property>
</bean>
添加
<!-- 自定义页面异常 出现异常会自动找到 MyExceptionResolver进行处理 -->
<bean class="review.resolver.MyExceptionResolver"></bean>