一.Spring MVC的异常处理
1.SimpleMappingExceptionResolver
适用范围:当项目中多处可能出现某种异常,并且解决异常的方式是跳转到指定的View组件时。
SimpleMappingExceptionResolver是一个类,需要在Spring的配置文件中使用节点进行注册。
2.@ExceptionHandler
在Controller内部,自定义处理异常的方法,并使用@ExceptionHandler进行注解!该方法的编写与处理请求的方法类似,然后,在方法的参数中应该添加Exception。
3.两种方式对比
(1)使用SimpleMappingExceptionResolver的配置很简单,但是处理得会比较“粗糙”,
很难提示精准的信息,除非异常本身出现的可能就很少!
(2)使用@ExceptionHandler比较自由,处理得可以更加“细致”,但是相对麻烦一些,毕竟每个处理异常的方法只能作用于它所在
的Controller类,如果某种异常在多个Controller类中都可能出现,则需要处理多次!
二.实例
2.web.xml配置文件
3.WEB-INF下的jsp的文件
4.控制器类
1.SimpleMappingExceptionResolver
适用范围:当项目中多处可能出现某种异常,并且解决异常的方式是跳转到指定的View组件时。
SimpleMappingExceptionResolver是一个类,需要在Spring的配置文件中使用节点进行注册。
2.@ExceptionHandler
在Controller内部,自定义处理异常的方法,并使用@ExceptionHandler进行注解!该方法的编写与处理请求的方法类似,然后,在方法的参数中应该添加Exception。
3.两种方式对比
(1)使用SimpleMappingExceptionResolver的配置很简单,但是处理得会比较“粗糙”,
很难提示精准的信息,除非异常本身出现的可能就很少!
(2)使用@ExceptionHandler比较自由,处理得可以更加“细致”,但是相对麻烦一些,毕竟每个处理异常的方法只能作用于它所在
的Controller类,如果某种异常在多个Controller类中都可能出现,则需要处理多次!
二.实例
1.spring配置文件applicationContext.xml
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<context:component-scan base-package="cn.tedu.spring"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 仅当以下代码删除时,本例中的@ExceptionHandler的作法才会有效! -->
<!-- <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.ArithmeticException">arithmeticException</prop>
<prop key="java.lang.ArrayIndexOutOfBoundsException">arrayException</prop>
</props>
</property>
</bean> -->
</beans>
2.web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>DAY06-02-SpringMVC-Exception</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3.WEB-INF下的jsp的文件
arithmeticException.jsp
<%@ page language="java"
contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8">
<title>Spring MVC - Exception</title>
</head>
<body>
<h2 style="color: red;">
java.lang.ArithmeticException
</h2>
</body>
</html>
<%@ page language="java"
contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8">
<title>Spring MVC - Exception</title>
</head>
<body>
<h2 style="color: red;">
数组下标越界啦!!!!!
</h2>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring MVC - Exception</title>
</head>
<body>
<h2 style="color:red;">${errorMessage}</h2>
</body>
</html>
4.控制器类
package cn.tedu.spring.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SampleController {
@ExceptionHandler
public String handlerException(Exception e,HttpServletRequest request) {
if (e instanceof ArithmeticException) {
request.setAttribute("errorMessage", "算术运算异常,你是不是拿某个数字除以0了?");
return "error";
}else if (e instanceof ArrayIndexOutOfBoundsException) {
request.setAttribute("errorMessage", "您使用的下标已经越界:"+e.getMessage());
return "error";
}else {
request.setAttribute("errorMessage", "未知异常请联系系统管理员");
return "error";
}
}
@RequestMapping("testException1.do")
public String testException1() {
int x = 10;
int y = 0;
System.out.println(x / y); // ArithmeticException
return "index";
}
@RequestMapping("testException2.do")
public String testException2() {
int[] numbers = { 9,5,2,7};
System.out.println(numbers[10]); // ArrayIndexOutOfBoundsException
return "index";
}
}