SpringMVC在处理请求过程中出现的异常可以交由异常处理器进行处理,而自定义异常处理器可以实现一个系统的异常处理逻辑。
Spring MVC内部提供了HandlerExceptionResolver的实现类SimpleMappingExceptionResolver,它实现了简单的异常处理,当发生异常的时候,该实现类根据发生的异常类型跳转到指定的页面处理异常信息。实现类也可以为所有的异常指定一个默认的异常处理页面,当应用程序抛出的异常没有对应的映射页面,则使用默认页面处理异常信息。
演示SimpleMappingExceptionResolver对异常的统一处理,案例具体实现步骤如下所示。
项目结构:
1.在controller包下创建ExceptionController类,编写几个处理器抛出不同的异常。ExceptionController类的具体代码如下所示。
@Controller
public class ExceptionController {
//抛出空指针异常
@RequestMapping("showNullPointer")
public void showNullPointer() {
ArrayList<Object> list = null;
System.out.println(list.get(2));
}
//抛出IO异常
@RequestMapping("showIOException")
public void showIOException() throws IOException {
FileInputStream in = new FileInputStream("JavaWeb.xml");
}
//抛出算术异常
@RequestMapping("showArithmetic")
public void showArithmetic() {
int c = 1 / 0;
}}
2.在Spring MVC的配置文件spring-mvc.xml中使用SimpleMappingExceptionResolver指定异常和异常处理页面的映射关系。Spring MVC配置文件的部分配置如下所示。
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置 Spring MVC要扫描的包 -->
<context:component-scan base-package="com.cqcet"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置注解驱动 -->
<mvc:annotation-driven/>
<!--配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 -->
<mvc:resources mapping="/js/**" location="/js/" />
<!-- 注入 SimpleMappingExceptionResolver-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 定义特殊处理的异常,类名或完全路径名作为key,对应的异常页面名作为值-->
<property name="exceptionMappings">
<props>
<prop key="java.lang.NullPointerException">nullPointerExp</prop>
<prop key="IOException">IOExp</prop>
</props>
</property>
<!-- 为所有的异常定义默认的异常处理页面,value为默认的异常处理页面 -->
<property name="defaultErrorView" value="defaultExp"></property>
<!-- value定义在异常处理页面中获取异常信息的变量名,默认名为exception -->
<property name="exceptionAttribute" value="exp"></property>
</bean>
</beans>
3.在目录webapp/pages下创建异常处理页面。在此不对异常处理页面做太多处理,只在页面中展示对应的异常信息。
nullPointerExp.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>空指针异常处理页面</title></head>
<body>
空指针异常处理页面-----${exp}
</body>
</html>
IOExp.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>IO异常处理页面</title></head>
<body>
IO异常处理页面-----${exp}
</body>
</html>
defaultExp.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>IO异常处理页面</title></head>
<body>
IO异常处理页面-----${exp}
</body>
</html>
4.启动springMVC项目,在浏览器中访问地址http://localhost:8080/springMVC/showNullPointer,程序将执行showNullPointer()方法。方法执行后页面显示效果如图所示。
如果根据以上操作进行运行,报如下错误:
这有些不是代码错误,是服务器错误了;建议打开Maven, 双击clean后,在双击install一下:
再重新运行就可以实现了。希望能帮助到大家,谢谢!!!😘