spring mvc 異常(runtime異常、ajax異常)統一處理與範例
SpringMVC 提供的異常處理主要有兩種方式,一種是直接實現自己的HandlerExceptionResolver,另一種是使用註解的方式實現一個專門用於處理異常的Controller——ExceptionHandler。前者當發生異常時,頁面會跳到指定的錯誤頁面,後者同樣,只是後者會在每個controller中都需要加入重複的程式碼。如何進行簡單地統一配置異常,使得發生普通錯誤指定到固定的頁面,ajax發生錯直接通過js獲取,展現給使用者,變得非常重要。下面先介紹下2種異常處理方式,同時,結合現有的程式碼,讓其支援ajax方式,實現spring MVC web系統的異常統一處理。
1、實現自己的HandlerExceptionResolver,HandlerExceptionResolver是一個介面,springMVC本身已經對其有了一個自身的實現——DefaultExceptionResolver,該解析器只是對其中的一些比較典型的異常進行了攔截處理 。
Java程式碼- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
import org.springframework.web.servlet.HandlerExceptionResolver;
- import org.springframework.web.servlet.ModelAndView;
- public class ExceptionHandler implements HandlerExceptionResolver {
- @Override
- public ModelAndView resolveException(HttpServletRequest request,
- HttpServletResponse response, Object handler, Exception ex) {
-
// TODO Auto-generated method stub
- return new ModelAndView("exception");
- }
- }
上述的resolveException的第4個引數表示對哪種型別的異常進行處理,如果想同時對多種異常進行處理,可以把它換成一個異常陣列。
定義了這樣一個異常處理器之後就要在applicationContext中定義這樣一個bean物件,如:
Xml程式碼- <bean id="exceptionResolver" class="com.tiantian.xxx.web.handler.ExceptionHandler"/>
2、使用@ExceptionHandler進行處理
使用@ExceptionHandler進行處理有一個不好的地方是進行異常處理的方法必須與出錯的方法在同一個Controller裡面
如:
Java程式碼- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.RequestMapping;
- import com.tiantian.blog.web.servlet.MyException;
- @Controller
- public class GlobalController {
- /**
- * 用於處理異常的
- * @return
- */
- @ExceptionHandler({MyException.class})
- public String exception(MyException e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- return "exception";
- }
- @RequestMapping("test")
- public void test() {
- throw new MyException("出錯了!");
- }
- }
這裡在頁面上訪問test方法的時候就會報錯,而擁有該test方法的Controller又擁有一個處理該異常的方法,這個時候處理異常的方法就會被呼叫。當發生異常的時候,上述兩種方式都使用了的時候,第一種方式會將第二種方式覆蓋。
3. 針對Spring MVC 框架,修改程式碼實現普通異常及ajax異常的全部統一處理解決方案。
在上篇文章中,關於spring異常框架體系講的非常清楚,Dao層,以及sevcie層異常我們建立如下異常。
Java程式碼- package com.jason.exception;
- public class BusinessException extends Exception {
- private static final long serialVersionUID = 1L;
- public BusinessException() {
- // TODO Auto-generated constructor stub
- }
- public BusinessException(String message) {
- super(message);
- // TODO Auto-generated constructor stub
- }
- public BusinessException(Throwable cause) {
- super(cause);
- // TODO Auto-generated constructor stub
- }
- public BusinessException(String message, Throwable cause) {
- super(message, cause);
- // TODO Auto-generated constructor stub
- }
- }
- package com.jason.exception;
- public class SystemException extends RuntimeException {
- private static final long serialVersionUID = 1L;
- public SystemException() {
- // TODO Auto-generated constructor stub
- }
- /**
- * @param message
- */
- public SystemException(String message) {
- super(message);
- // TODO Auto-generated constructor stub
- }
- /**
- * @param cause
- */
- public SystemException(Throwable cause) {
- super(cause);
- // TODO Auto-generated constructor stub
- }
- /**
- * @param message
- * @param cause
- */
- public SystemException(String message, Throwable cause) {
- super(message, cause);
- // TODO Auto-generated constructor stub
- }
- }
在sevice層我們需要將建立的異常丟擲,在controller層,我們需要捕捉異常,將其轉換直接丟擲,丟擲的異常,希望能通過我們自己統一的配置,支援普通頁面和ajax方式的頁面處理,下面就詳細講一下步驟。
(1) 配置web.xml 檔案,將常用的異常進行配置,配置檔案如下403,404,405,500頁面都配置好了:
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" id="WebApp_ID" version="2.5">