1. 程式人生 > >spring mvc 異常(runtime異常、ajax異常)統一處理與範例

spring mvc 異常(runtime異常、ajax異常)統一處理與範例

SpringMVC 提供的異常處理主要有兩種方式,一種是直接實現自己的HandlerExceptionResolver,另一種是使用註解的方式實現一個專門用於處理異常的Controller——ExceptionHandler。前者當發生異常時,頁面會跳到指定的錯誤頁面,後者同樣,只是後者會在每個controller中都需要加入重複的程式碼。如何進行簡單地統一配置異常,使得發生普通錯誤指定到固定的頁面,ajax發生錯直接通過js獲取,展現給使用者,變得非常重要。下面先介紹下2種異常處理方式,同時,結合現有的程式碼,讓其支援ajax方式,實現spring MVC web系統的異常統一處理。

1、實現自己的HandlerExceptionResolver,HandlerExceptionResolver是一個介面,springMVC本身已經對其有了一個自身的實現——DefaultExceptionResolver,該解析器只是對其中的一些比較典型的異常進行了攔截處理 。

Java程式碼  收藏程式碼
  1. import javax.servlet.http.HttpServletRequest;  
  2. import javax.servlet.http.HttpServletResponse;  
  3. import org.springframework.web.servlet.HandlerExceptionResolver;  
  4. import org.springframework.web.servlet.ModelAndView;  
  5. public class ExceptionHandler implements HandlerExceptionResolver {  
  6.     @Override  
  7.     public ModelAndView resolveException(HttpServletRequest request,  
  8.             HttpServletResponse response, Object handler, Exception ex) {  
  9.         // TODO Auto-generated method stub
      
  10.         return new ModelAndView("exception");  
  11.     }  
  12. }  

 上述的resolveException的第4個引數表示對哪種型別的異常進行處理,如果想同時對多種異常進行處理,可以把它換成一個異常陣列。

定義了這樣一個異常處理器之後就要在applicationContext中定義這樣一個bean物件,如:

Xml程式碼  收藏程式碼
  1. <bean id="exceptionResolver" class="com.tiantian.xxx.web.handler.ExceptionHandler"/>  

2、使用@ExceptionHandler進行處理

使用@ExceptionHandler進行處理有一個不好的地方是進行異常處理的方法必須與出錯的方法在同一個Controller裡面

如:

Java程式碼  收藏程式碼
  1. import org.springframework.stereotype.Controller;  
  2. import org.springframework.web.bind.annotation.ExceptionHandler;  
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. import com.tiantian.blog.web.servlet.MyException;  
  5. @Controller  
  6. public class GlobalController {  
  7.     /** 
  8.      * 用於處理異常的 
  9.      * @return 
  10.      */  
  11.     @ExceptionHandler({MyException.class})  
  12.     public String exception(MyException e) {  
  13.         System.out.println(e.getMessage());  
  14.         e.printStackTrace();  
  15.         return "exception";  
  16.     }  
  17.     @RequestMapping("test")  
  18.     public void test() {  
  19.         throw new MyException("出錯了!");  
  20.     }  
  21. }  

這裡在頁面上訪問test方法的時候就會報錯,而擁有該test方法的Controller又擁有一個處理該異常的方法,這個時候處理異常的方法就會被呼叫。當發生異常的時候,上述兩種方式都使用了的時候,第一種方式會將第二種方式覆蓋。

3. 針對Spring MVC 框架,修改程式碼實現普通異常及ajax異常的全部統一處理解決方案。

在上篇文章中,關於spring異常框架體系講的非常清楚,Dao層,以及sevcie層異常我們建立如下異常。

Java程式碼  收藏程式碼
  1. package com.jason.exception;  
  2. public class BusinessException extends Exception {  
  3.     private static final long serialVersionUID = 1L;  
  4.     public BusinessException() {  
  5.         // TODO Auto-generated constructor stub  
  6.     }  
  7.     public BusinessException(String message) {  
  8.         super(message);  
  9.         // TODO Auto-generated constructor stub  
  10.     }  
  11.     public BusinessException(Throwable cause) {  
  12.         super(cause);  
  13.         // TODO Auto-generated constructor stub  
  14.     }  
  15.     public BusinessException(String message, Throwable cause) {  
  16.         super(message, cause);  
  17.         // TODO Auto-generated constructor stub  
  18.     }  
  19. }  
Java程式碼  收藏程式碼
  1. package com.jason.exception;  
  2. public class SystemException extends RuntimeException {  
  3.     private static final long serialVersionUID = 1L;  
  4.     public SystemException() {  
  5.         // TODO Auto-generated constructor stub  
  6.     }  
  7.     /** 
  8.      * @param message 
  9.      */  
  10.     public SystemException(String message) {  
  11.         super(message);  
  12.         // TODO Auto-generated constructor stub  
  13.     }  
  14.     /** 
  15.      * @param cause 
  16.      */  
  17.     public SystemException(Throwable cause) {  
  18.         super(cause);  
  19.         // TODO Auto-generated constructor stub  
  20.     }  
  21.     /** 
  22.      * @param message 
  23.      * @param cause 
  24.      */  
  25.     public SystemException(String message, Throwable cause) {  
  26.         super(message, cause);  
  27.         // TODO Auto-generated constructor stub  
  28.     }  
  29. }  

在sevice層我們需要將建立的異常丟擲,在controller層,我們需要捕捉異常,將其轉換直接丟擲,丟擲的異常,希望能通過我們自己統一的配置,支援普通頁面和ajax方式的頁面處理,下面就詳細講一下步驟。

(1) 配置web.xml 檔案,將常用的異常進行配置,配置檔案如下403,404,405,500頁面都配置好了:

Xml程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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" id="WebApp_ID" version="2.5">  
  3.