1 描述
在J2EE項目的開發中,不管是對底層的數據庫操作過程,還是業務層的處理過程,還是控制層的處理過程,都不可避免會遇到各種可預知的、不可預知的異常需要處理。每個過程都單獨處理異常,系統的代碼耦合度高,工作量大且不好統一,維護的工作量也很大。
那麼,能不能將所有類型的異常處理從各處理過程解耦出來,這樣既保證了相關處理過程的功能較單一,也實現了異常信息的統一處理和維護?答案是肯定的。下面將介紹使用Spring MVC統一處理異常的解決和實現過程。
2 分析
Spring MVC處理異常有3種方式:
(1)使用Spring MVC提供的簡單異常處理器SimpleMappingExceptionResolver;
(2)實現Spring的異常處理SimpleMappingExceptionResolver自定義自己的異常處理器;
(3)實現HandlerExceptionResolver 接口自定義異常處理器
(4)使用注解@ExceptionHandler實現異常處理;
3 實戰
一:使用Spring MVC提供的簡單異常處理器SimpleMappingExceptionResolver
源碼介紹:
1.lib包(jar包)和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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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></display-name> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>View Code
2.index.jsp(測試頁面入口)和 error.jsp(有錯誤則會跳到此頁面)和 hello.jsp(沒錯誤則會跳到此頁面)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>異常處理器測試</title> </head> <body> <form action="frist.do" method="post"> <input type="submit" value="測試" /> </form> </body> </html>View Code
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>這是錯誤頁面</title> </head> <body> 這是錯誤頁面 ${ex.message } </body> </html>View Code
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>成功歡迎頁面</title> </head> <body> 你竟然沒報錯<br/> </body> </html>View Code
3.MyController,java(定義自己的處理器)
package cn.zhang.controller; //定義自己的處理器 import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class MyController{ @RequestMapping(value="/frist.do",produces="text/html;charset=utf-8",method=RequestMethod.POST) public String frist(){ //制造一個異常 int i=5/0; System.out.println(i); return "forward:/hello.jsp"; } }View Code
4.applicationContext.xml(Spring的配置文件)
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 使用注解方式完成映射 --> <context:component-scan base-package="cn.zhang.controller"></context:component-scan> <!-- mvc的注解驅動 --> <mvc:annotation-driven /> <!-- 注冊系統異常處理器 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error.jsp"></property> <property name="exceptionAttribute" value="ex"></property> </bean> </beans>View Code
測試展示:
點擊測試,由於我們在自己的處理器制造了一個異常,所以它會跳到錯誤頁面
二:實現Spring的異常處理接口SimpleMappingExceptionResolver自定義自己的異常處理器
源碼介紹:
1.lib包和web.xml一樣(不做解釋)
2.error包中是指定錯誤頁面
ageerrors.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>年齡錯誤頁面</title> </head> <body>年齡錯誤 ${ex.message } </body> </html>View Code
nameerrors.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>名字錯誤頁面</title> </head> <body> 名字錯誤 ${ex.message } </body> </html>View Code
3.MyController.java
package cn.zhang.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import cn.zhang.exception.AgeException; import cn.zhang.exception.NameException; import cn.zhang.exception.UserException; //定義自己的處理器 @Controller public class MyController{ @RequestMapping(value="/frist.do") public String frist(Model model,String name,int age) throws UserException{ if (name.equals("admin")) { throw new NameException("用戶名錯誤"); } if (age>50) { throw new AgeException("年齡過大"); } return "forward:/hello.jsp"; } }View Code
4.exception包下,指定我們的異常類
UserException.java
package cn.zhang.exception; //定義UserException繼承Exception public class UserException extends Exception { private static final long serialVersionUID = 1L; public UserException() { super(); // TODO Auto-generated constructor stub } public UserException(String message) { super(message); // TODO Auto-generated constructor stub } }View Code
AgeException.java
package cn.zhang.exception; //繼承UserException父類 public class AgeException extends UserException { private static final long serialVersionUID = 1L; public AgeException() { super(); // TODO Auto-generated constructor stub } public AgeException(String message) { super(message); // TODO Auto-generated constructor stub } }View Code
NameException.java
package cn.zhang.exception; //繼承UserException父類 public class NameException extends UserException { private static final long serialVersionUID = 1L; public NameException() { super(); // TODO Auto-generated constructor stub } public NameException(String message) { super(message); // TODO Auto-generated constructor stub } }View Code
5.applicationContext.xml(Spring的配置文件)
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 使用注解方式完成映射 --> <context:component-scan base-package="cn.zhang.controller"></context:component-scan> <!-- mvc的注解驅動 --> <mvc:annotation-driven /> <!-- 實現Spring的異常處理接口HandlerExceptionResolver 自定義自己的異常處理器 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error.jsp"></property> <property name="exceptionAttribute" value="ex"></property> <!-- 指定錯誤到指定頁面 --> <property name="exceptionMappings"> <props> <prop key="cn.zhang.exception.AgeException">error/ageerrors.jsp</prop> <prop key="cn.zhang.exception.NameException">error/nameerrors.jsp</prop> </props> </property> </bean> </beans>View Code
結果展示:
三:實現HandlerExceptionResolver 接口自定義異常處理器
要修改的代碼:
1.MyHandlerExceptionResolver.java--定義自己的異常處理器(實現HandlerExceptionResolver接口)
package cn.zhang.resolvers; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; import cn.zhang.exception.AgeException; import cn.zhang.exception.NameException; /** * 定義自己的異常處理器(實現HandlerExceptionResolver接口) * @author zhangzong * */ public class MyHandlerExceptionResolver implements HandlerExceptionResolver{ public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ModelAndView mv=new ModelAndView(); mv.addObject("ex",ex); mv.setViewName("/errors.jsp"); if(ex instanceof NameException){ mv.setViewName("/error/nameerrors.jsp"); } if(ex instanceof AgeException){ mv.setViewName("/error/ageerrors.jsp"); } return mv; } }View Code
2.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 使用注解方式完成映射 --> <context:component-scan base-package="cn.zhang.controller"></context:component-scan> <!-- mvc的注解驅動 --> <mvc:annotation-driven /> <!-- 注冊自定義異常處理器 --> <bean class="cn.zhang.resolvers.MyHandlerExceptionResolver"/> </beans>View Code
其他的相同,不作解釋
四:使用注解@ExceptionHandler實現異常處理
源碼介紹:
1.其他配置相同(不做解釋)
2.MyController.java--繼承我們自己定義的注解異常處理器MyHandlerExceptionResolver
package cn.zhang.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import cn.zhang.exception.AgeException; import cn.zhang.exception.NameException; import cn.zhang.exception.UserException; import cn.zhang.resolvers.MyHandlerExceptionResolver; //定義自己的處理器 //繼承我們自己定義的注解異常處理器MyHandlerExceptionResolver @Controller public class MyController extends MyHandlerExceptionResolver{ @RequestMapping(value="/frist.do") public String frist(Model model,String name,int age) throws UserException{ if (name.equals("admin")) { throw new NameException("用戶名錯誤"); } if (age>50) { throw new AgeException("年齡過大"); } return "forward:/hello.jsp"; } }View Code
3.MyHandlerExceptionResolver.java--定義自己的異常處理器(使用注解)
package cn.zhang.resolvers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.servlet.ModelAndView; import cn.zhang.exception.AgeException; import cn.zhang.exception.NameException; /** * 定義自己的異常處理器(使用注解) * @author zhangzong * */ @Controller public class MyHandlerExceptionResolver{ @ExceptionHandler public ModelAndView resolveException(Exception ex) { ModelAndView mv=new ModelAndView(); mv.addObject("ex",ex); mv.setViewName("/errors.jsp"); if(ex instanceof NameException){ mv.setViewName("/error/nameerrors.jsp"); } if(ex instanceof AgeException){ mv.setViewName("/error/ageerrors.jsp"); } return mv; } }View Code
4.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 使用注解方式完成映射 --> <context:component-scan base-package="cn.zhang.controller"></context:component-scan> <!-- mvc的注解驅動 --> <mvc:annotation-driven /> <!-- 注冊自定義異常處理器 --> <bean class="cn.zhang.resolvers.MyHandlerExceptionResolver"/> </beans>View Code
效果和上相同,這裡不做展示