当异常被直接抛到浏览器,页面上显示一大堆错误堆栈信息,用户看到这些错误堆栈信息,肯定觉得我们这个系统设计的不友好,而且错误堆栈信息一旦暴露了,会存在着一定的潜在风险,虽然之前也学过web.xml中可以配置处理异常的Jsp页面,但是这还远远不够,SpringMVC对错误处理提供了更好的解决方案。
SpringMVC中提供的异常处理方式有两种
1)使用SpringMVC提供的简单异常处理器SimpleMappingExceptionResolver
2)使用@ExceptionHandler注解实现局部异常处理或使用@ControllerAdvice注解实现统一异常处理
SpringMVC中提供的异常处理方式 |
---|
①使用SpringMVC提供的简单异常处理器SimpleMappingExceptionResolver |
②使用 @ExceptionHandler注解实现局部异常处理或使用 @ControllerAdvice注解实现统一异常处理 |
一、SimpleMappingExceptionResolver进行异常处理
视图解析器可以将你的路径简写,可以将你的前缀和后缀在加你的名字就是你的实际路径。
代码
@Controller
public class TestExceptionontroller {
@RequestMapping(value = "/testException1")
public String testException1() throws Exception {
throw new Exception();
}
@RequestMapping(value = "/testException2")
public String testException2() {
//模拟异常
int i = 10 / 0;
return "success";
}
@RequestMapping(value = "/testException3")
public String testException3() throws Exception {
try {
//模拟异常
int i = 10 / 0;
return "suceess";
} catch (Exception e) {
throw new SQLException("查找数据失败");
}
}
}
springmvc配置文件
<?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">
<context:component-scan base-package="com.acoffee.maven.controller"></context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/pages/"></property>
<!--后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--第一种方式:配置springmvc异常处理器-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"></property>
<property name="exceptionAttribute" value="ex"></property>
<property name="exceptionMappings">
<props>
<prop key="ArithmeticException">arth</prop>
<prop key="IOException">IOEx</prop>
</props>
</property>
</bean>
</beans>
arth.sjp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
出现了算数异常
</body>
</html>
IOEx.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
出现了IO异常
</body>
</html>
error.sjp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>异常界面</title>
</head>
<body>
您的页面出错了,错误信息是
<h2>${ex.message}</h2>
</body>
</html>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<hr>
<a href="testException1">没有异常处理</a>
<a href="testException2">使用简单异常处理器处理异常</a>
<a href="testException3">使用简单异常处理器处理特定异常</a>
</body>
</html>
执行结果:
二、@ExceptionHandler注解实现局部异常处理
@Controller
public class TestExceptionontroller {
@ExceptionHandler(value = Exception.class)
public ModelAndView handlerError(Exception ex){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("ex",ex);
modelAndView.setViewName("error");
return modelAndView;
}
}
springmvc配置文件
<context:component-scan base-package="com.acoffee.maven.controller"></context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/pages/"></property>
<!--后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
但是这个方法只能处理局部,因为你每处理一个异常都要加一次注解,我们要是用这种方式一次性处理整个系统的错误的话,上升到全局的操作@ExceptionHandler就不能完全解决问题了,所以我就引入新的注解@ControllerAdvice注解实现统一异常处理。
三、@ControllerAdvice注解实现统一异常处理
我们直接建立一个exception的包将这个类放入其中
@ControllerAdvice
public class GlobalException {
@ExceptionHandler(value = Exception.class)
public ModelAndView handlerError(Exception ex){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("ex",ex);
modelAndView.setViewName("error");
return modelAndView;
}
}