一、概述

承接上文十一(一)、springMVC之异常处理@ExceptionHandler注解

SimpleMappingExceptionResolver的作用:对所有的异常进行统一处理,他将异常类名映射为视图名,即发生异常时,使用对应的视图显示异常;

承接上文:

SimpleMappingExceptionResolver的配置,举例说明:

针对Java.lang.ArrayIndexOutOfBoundsException该异常配置的错误,如果页面跳转到了该页面,那么就会跳转到 视图解析器前缀+myerror+视图解析器后缀

如果报其他错误,则默认跳转到 视图解析器前缀+error+视图解析器后缀

获取异常信息,默认参数为exception,如果配置了exceptionAttribute,如下 value值为 ex,那么页面去${ex}即可;

1 <!-- 配置使用SimpleMappingExceptionResolver -->
2 <bean
3 class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
4 <!-- 定义默认的异常处理页面,当该异常类型注册时使用 -->
5 <property name="defaultErrorView" value="error"></property>
6 <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->
7 <property name="exceptionAttribute" value="ex"></property>
8 <!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常页名作为值 -->
9 <property name="exceptionMappings">
10 <props>
11 <prop key="java.lang.ArrayIndexOutOfBoundsException">myerror</prop>
12 <!-- 在这里还可以继续扩展对不同异常类型的处理 -->
13 </props>
14 </property>
15 </bean>

二、实例验证:

目录结果:

 

配置:

web.xml配置:只配置了DispatcherServlet

十一(三)、springMVC之异常处理SimpleMappingExceptionResolver_mvc

十一(三)、springMVC之异常处理SimpleMappingExceptionResolver_spring_02

1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns="http://xmlns.jcp.org/xml/ns/javaee"
4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5 id="WebApp_ID" version="3.1">
6 <servlet>
7 <servlet-name>springDispatcherServlet</servlet-name>
8 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
9 <!-- 配置DispatcherServletd 一个初始化参数:配置springmvc配置文件的位置和名称 -->
10 <!-- 实际上也可以不通过 contextConfigLocation 来配置Springmvc的配置文件,而是用默认的 即默认的配置文件为
11 /WEB-INF/<servlet-name>-servlet.xml 本项目默认位置配置文件即为: /WEB-INF/springDispatcherServlet-servlet.xml -->
12 <init-param>
13 <param-name>contextConfigLocation</param-name>
14 <param-value>classpath:spring.xml</param-value>
15 </init-param>
16 <!-- 表示springDispatcherServlet在加载的时候被创建 -->
17 <load-on-startup>1</load-on-startup>
18 </servlet>
19
20 <!-- Map all requests to the DispatcherServlet for handling -->
21 <servlet-mapping>
22 <servlet-name>springDispatcherServlet</servlet-name>
23 <url-pattern>/</url-pattern>
24 </servlet-mapping>
25 </web-app>

View Code

spring.xmp配置:基础配置+SimpleMappingExceptionResolver配置

基础配置包括:包扫描的配置、视图解析器配置、<mvc:annotation-driven></mvc:annotation-driven>;

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:mvc="http://www.springframework.org/schema/mvc"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
9
10 <context:component-scan base-package="handler"></context:component-scan>
11 <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
12 <bean
13 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
14 <property name="prefix" value="/WEB-INF/views/"></property>
15 <property name="suffix" value=".jsp"></property>
16 </bean>
17
18 <mvc:annotation-driven></mvc:annotation-driven>
19 <!-- 配置使用SimpleMappingExceptionResolver -->
20 <bean
21 class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
22 <!-- 定义默认的异常处理页面,当该异常类型注册时使用 -->
23 <property name="defaultErrorView" value="error"></property>
24 <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->
25 <property name="exceptionAttribute" value="ex"></property>
26 <!-- 定义需要特殊处理的异常,用类名或完全路径名作为key,异常页名作为值 -->
27 <property name="exceptionMappings">
28 <props>
29 <prop key="java.lang.ArrayIndexOutOfBoundsException">myerror</prop>
30 <!-- 在这里还可以继续扩展对不同异常类型的处理 -->
31 </props>
32 </property>
33 </bean>
34 </beans>

EceptionController.java

1 package handler;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.web.bind.annotation.RequestParam;
6
7 @RequestMapping("/emp")
8 @Controller
9 public class EceptionController {
10
11 @RequestMapping("/testSimpleMappingExceptionResolver")
12 public String testSimpleMappingExceptionResolver(@RequestParam("i") int i) {
13 String[] arr = new String[9];
14 System.out.println(arr[i]);
15 System.out.println("testSimpleMappingExceptionResolver");
16 return "success";
17 }
18
19 @RequestMapping("/testError")
20 public String testError(@RequestParam("i") int i) {
21 System.out.println(10 / i);
22 return "success";
23 }
24

 

错误页面:

error.jsp;

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta charset="UTF-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <h4>Error Page</h4>
11 ${ex}
12 </body>
13 </html>

View Code

myerror.jsp

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <meta charset="UTF-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <h4>Error Page MY ERROR</h4>
11 ${ex}
12 </body>
13 </html>

View Code

运行结果:

访问:http://localhost:8080/DataOperate/emp/testError?i=0,跳转到默认的error.jsp页面,并显示异常信息,如下图所示:

十一(三)、springMVC之异常处理SimpleMappingExceptionResolver_mvc_03

访问:http://localhost:8080/DataOperate/emp/testSimpleMappingExceptionResolver?i=13,跳转到配置的特定的错误页面myerror.jsp,并且显示异常信息,如下图所示

十一(三)、springMVC之异常处理SimpleMappingExceptionResolver_xml_04

 

注意:

  • 如果把spring.xml中配置的<property name="exceptionAttribute" value="ex"></property> 去掉,则两个错误页面获取异常信息的值需要修改为exception;
  • 配置的错误页面,经过视图解析器,也就是说, 视图解析器前缀+myerror+视图解析器后缀

 


 

我从来不相信什么懒洋洋的自由。我向往的自由是通过勤奋和努力实现的更广阔的人生。 我要做一个自由又自律的人,靠势必实现的决心认真地活着。