- SpringMVC 中异常的处理方式
- 系统的 Dao、Service、Controller 出现都通过 throws Exception 向上抛出,最后由 SpringMVC 前端控制器交 由异常处理器进行异常处理,如下图

- 两种处理的方式
- 使用 Spring MVC 提供的简单异常处理器 SimpleMappingExceptionResolver
- 实现 Spring 的异常处理接口 HandlerExceptionResolver 自定义自己的异常处理器
- 定义实现了 HandlerExceptionResolver 接口的异常处理器
- 将该类
- 定义异常页面
代码
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>priv.noby</groupId>
<artifactId>springMVC7</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties> <maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties> <dependencies> <dependency> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.4</version>
</dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.4</version>
</dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.4</version>
</dependency>
<dependency> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency> </dependencies>
<build> <plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration> <port>8080</port>
<path>/springMVC7</path>
<uriEncoding>utf-8</uriEncoding>
</configuration> </plugin> </plugins> </build></project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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>
</web-app>
spring-mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="priv.noby.springmvc7.controller"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class="priv.noby.springmvc7.resolver.MyExceptionResolver"/>
</beans>
exception
package priv.noby.springmvc7.exception;
public class MyException extends RuntimeException{
}
resolver
package priv.noby.springmvc7.resolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import priv.noby.springmvc7.exception.MyException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView modelAndView = new ModelAndView();
if(e instanceof MyException){
modelAndView.setViewName("/my_exception_error.jsp");
}else if(e instanceof ClassCastException){
modelAndView.setViewName("/class_cast_error.jsp");
}else {
modelAndView.setViewName("/default_error.jsp");
}
return modelAndView;
}
}
controller
package priv.noby.springmvc7.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import priv.noby.springmvc7.exception.MyException;
@Controller
@RequestMapping("/student")
public class StudentController {
@GetMapping("/selectById")
public ModelAndView selectById(ModelAndView modelAndView) {
System.out.println("StudentController.selectById");
Object str = "noby";
Integer num = (Integer)str;
modelAndView.setViewName("forward:/student.jsp");
modelAndView.addObject("info","模拟成功返回了一条学生信息");
return modelAndView;
}
@GetMapping("/selectById2")
public ModelAndView selectById2(ModelAndView modelAndView) {
System.out.println("StudentController.selectById2");
int i = 1/0;
modelAndView.setViewName("forward:/student.jsp");
modelAndView.addObject("info","模拟成功返回了一条学生信息");
return modelAndView;
}
@GetMapping("/selectById3")
public ModelAndView selectById3(ModelAndView modelAndView) {
System.out.println("StudentController.selectById3");
if (true) {
throw new MyException();
}
modelAndView.setViewName("forward:/student.jsp");
modelAndView.addObject("info","模拟成功返回了一条学生信息");
return modelAndView;
}
}
http
### 测试类转换异常捕获
GET http://localhost:8080/springMVC7/student/selectById
### 测试数学异常(跳转到默认异常页面)捕获
GET http://localhost:8080/springMVC7/student/selectById2
### 测试自定义异常捕获
GET http://localhost:8080/springMVC7/student/selectById3
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>class_cast_error</title>
</head>
<body>
类转换异常
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>default_error</title>
</head>
<body>
默认的异常
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>my_exception_error</title>
</head>
<body>
自定义异常
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>student</title>
</head>
<body>
成功访问没有异常!!!
${requestScope.info}
</body>
</html>