十一(三)、springMVC之异常处理SimpleMappingExceptionResolver
一、概述
承接上文
SimpleMappingExceptionResolver的作用:对所有的异常进行统一处理,他将异常类名映射为视图名,即发生异常时,使用对应的视图显示异常;
承接上文:
SimpleMappingExceptionResolver的配置,举例说明:
针对Java.lang.ArrayIndexOutOfBoundsException该异常配置的错误,如果页面跳转到了该页面,那么就会跳转到 视图解析器前缀+myerror+视图解析器后缀
如果报其他错误,则默认跳转到 视图解析器前缀+error+视图解析器后缀
获取异常信息,默认参数为exception,如果配置了exceptionAttribute,如下 value值为 ex,那么页面去${ex}即可;
1 2 <bean 3 class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 4 5 <property name="defaultErrorView" value="error">property> 6 7 <property name="exceptionAttribute" value="ex">property> 8 9 <property name="exceptionMappings"> 10 <props> 11 <prop key="java.lang.ArrayIndexOutOfBoundsException">myerrorprop> 12 13 props> 14 property> 15 bean>
二、实例验证:
目录结果:
配置:
web.xml配置:只配置了DispatcherServlet
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>springDispatcherServletservlet-name> 8 <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> 9 10 12 <init-param> 13 <param-name>contextConfigLocationparam-name> 14 <param-value>classpath:spring.xmlparam-value> 15 init-param> 16 17 <load-on-startup>1load-on-startup> 18 servlet> 19 20 21 <servlet-mapping> 22 <servlet-name>springDispatcherServletservlet-name> 23 <url-pattern>/url-pattern> 24 servlet-mapping> 25 web-app>
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 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 20 <bean 21 class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 22 23 <property name="defaultErrorView" value="error">property> 24 25 <property name="exceptionAttribute" value="ex">property> 26 27 <property name="exceptionMappings"> 28 <props> 29 <prop key="java.lang.ArrayIndexOutOfBoundsException">myerrorprop> 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 heretitle> 8 head> 9 <body> 10 <h4>Error Pageh4> 11 ${ex} 12 body> 13 html>
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 heretitle> 8 head> 9 <body> 10 <h4>Error Page MY ERRORh4> 11 ${ex} 12 body> 13 html>
运行结果:
访问:http://localhost:8080/DataOperate/emp/testError?i=0,跳转到默认的error.jsp页面,并显示异常信息,如下图所示:

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

注意:
- 如果把spring.xml中配置的
去掉,则两个错误页面获取异常信息的值需要修改为exception; - 配置的错误页面,经过视图解析器,也就是说, 视图解析器前缀+myerror+视图解析器后缀