Spring MVC 系統異常處理方式及性能對比

大部分公司所用的Spring框架版本是3.1版本如下,因此今天暫時總結3.1版本的Spring-MVC異常處理方式。javascript

1、Spring MVC處理異常有3種方式: 
(1)使用Spring-MVC提供的SimpleMappingExceptionResolver; 
(2)實現Spring的異常處理接口HandlerExceptionResolver 自定義本身的異常處理器; 
(3)使用@ExceptionHandler註解實現異常處理; 
html

2、分別介紹這三種異常處理的實現方式:java

(1)使用SimpleMappingExceptionResolver實現異常處理jquery

          只須要在Spring的配置文件applicationContext.xml中增長如下內容:web

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
    <!-- 定義默認的異常處理頁面,當該異常類型的註冊時使用 -->  
    <property name="defaultErrorView" value="error"></property>  
    <!-- 定義異常處理頁面用來獲取異常信息的變量名,默認名爲exception -->  
    <property name="exceptionAttribute" value="ex"></property>  
    <!-- 定義須要特殊處理的異常,用類名或徹底路徑名做爲key,異常以頁名做爲值 -->  
    <property name="exceptionMappings">  
        <props>  
            <!-- 建立本身所要自定義的異常類 -->
            <prop key="com.core.exception.BusinessException">business_error</prop>  
            <prop key="com.core.exception.ParameterException">parameter_error</prop>  
            <!-- 還能夠繼續擴展對不一樣異常類型的處理 -->  
        </props>  
    </property>  
</bean>


(2) 實現HandlerExceptionResolver 接口自定義異常處理器ajax

  首先增長HandlerExceptionResolver 接口的實現類MyExceptionHandler,代碼以下: spring

public class MyExceptionHandler implements HandlerExceptionResolver {  
  
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, 
        Object handler, Exception ex) {  
        Map<String, Object> model = new HashMap<String, Object>();  
        model.put("ex", ex);  
        // 根據不一樣錯誤轉向不一樣頁面  
        if(ex instanceof BusinessException) {  
            return new ModelAndView("business_error", model);  
        }else if(ex instanceof ParameterException) {  
            return new ModelAndView("parameter_error", model);  
        } else {  
            return new ModelAndView("error", model);  
        }  
    }  
}


    而後在Spring的配置文件applicationContext.xml中增長如下內容:json

<bean id="exceptionHandler" class="com.core.exception.MyExceptionHandler"/>


   (3)使用@ExceptionHandler註解實現異常處理 服務器

     首先要增長BaseController類,並在類中使用@ExceptionHandler註解聲明異常處理,代碼以下:app

public class BaseController {  
    /** 基於@ExceptionHandler異常處理 */  
    @ExceptionHandler  
    public String exp(HttpServletRequest request, Exception ex) {  
        request.setAttribute("ex", ex);  
        // 根據不一樣錯誤轉向不一樣頁面  
        if(ex instanceof BusinessException) {  
            return "business_error";  
        }else if(ex instanceof ParameterException) {  
            return "parameter_error";  
        } else {  
            return "error";  
        }  
    }  
}


    而後須要修改現有代碼,使全部須要異常處理的Controller都繼承該類,以下所示:

public class TestController extends BaseController

3、未捕獲異常的處理 :
        對於Unchecked Exception而言,因爲代碼不強制捕獲,每每被忽略,若是運行期產生了UncheckedException,而代碼中又沒有進行相應的捕獲和處理,則咱們可能不得不面對尷尬的40四、500……等服務器內部錯誤提示頁面。 
        咱們須要一個全面而有效的異常處理機制。目前大多數服務器也都支持在Web.xml中經過<error-page>(Websphere/Weblogic)或者<error-code>(Tomcat)節點配置特定異常狀況的顯示頁面。

實現方式以下:

修改web.xml文件,增長如下內容: 

<!-- 出錯頁面定義 -->  
<error-page>  
    <exception-type>java.lang.Throwable</exception-type>  
    <location>/500.jsp</location>  
</error-page>  
<error-page>  
    <error-code>500</error-code>  
    <location>/500.jsp</location>  
</error-page>  
<error-page>  
    <error-code>404</error-code>  
    <location>/404.jsp</location>  
</error-page>  
<!-- 可繼續增長服務器錯誤號的處理及對應顯示頁面 -->


4、比較異常處理方式的優缺點:

        Spring MVC集成異常處理3種方式均可以達到統一異常處理的目標。從3種方式的優缺點比較,若只須要簡單的集成異常處理,推薦使用SimpleMappingExceptionResolver便可;若須要集成的異常處理可以更具個性化,提供給用戶更詳細的異常信息,推薦自定義實現HandlerExceptionResolver接口的方式;若不喜歡Spring配置文件或要實現「零配置」,且能接受對原有代碼的適當入侵,則建議使用@ExceptionHandler註解方式。 


在此補充個Ajax實現異常信息無跳轉的處理方式:

   Java代碼(Controller類)

  @RequestMapping(value = "/ajaxAlert", method = RequestMethod.POST)  
    public void ajax(HttpServletResponse response, User user) throws Exception {  

		if(user.getId()==null) {  
            if(user.getUserName()==null || "".equals(user.getUserName())) {  
		AjaxUtils.rendJson(response, false, "用戶名爲空建立失敗"); 
            } else {  
                AjaxUtils.rendJson(response, true, "建立用戶成功");  
            }  
        } else {  
            AjaxUtils.rendJson(response, true, "修改用戶成功");  
        }
    }
    
     @RequestMapping(value = "/controllerAjax", method = RequestMethod.POST)
	public void controllerAjax(HttpServletResponse response, Integer id) throws Exception {    
        try { 
            testService.daoTest(); 
            AjaxUtils.rendJson(response, true, "操做成功");    
        } catch(Exception be) {    
	    AjaxUtils.rendJson(response, false, "Dao層異常");		
        }    
    }


  Java代碼(AjaxUtils類)

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;

public class AjaxUtils {

    public static void rendText(HttpServletResponse response, String content)  
        throws IOException {  
        response.setCharacterEncoding("UTF-8");  
        response.getWriter().write(content);  
    }    
      
    public static void rendJson(HttpServletResponse response, boolean success,
         String message) throws IOException{  
        JSONObject json = new JSONObject();  
        json.put("isSuccess", success);  
        json.put("message", message);  
        rendText(response, json.toString());  
    }  	
}

    html代碼(jQuery)

<!DOCTYPE html>  
<html>  
 <head>  
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
  <title>Exception</title>  
  <script type="text/javascript" src="./jquery-1.10.2.min.js"></script>  
  <script type="text/javascript">  
  $(function(){  
        init();  
    });  
    
  function init(){  
        $("#ajaxCreate").click(function(){  
            doAjax({userName: "Candy", realName: "Carol"});  
        })  
        $("#ajaxUpdate").click(function(){  
            doAjax({id: 1, userName: "Candy", realName: "Carol"});  
        })  
        $("#ajaxFail").click(function(){  
            doAjax({realName: "Carol"});  
        })  
    }  
  function doAjax(data) {  
      $.post("./ajax.do",data,function(t){  
            if(!t.isSuccess){  
                alert("操做失敗, 緣由:" + t.message);  
            }else{  
                alert("操做成功, 描述:" + t.message);  
            }  
        },"json").error(function(){  
            alert("未知錯誤");  
        });  
  }  
  </script>  
 </head>  
<body>  
  <br />  
  <a id="ajaxCreate" href="#">建立用戶成功</a>  
  <br />  
  <a id="ajaxUpdate" href="#">修改用戶成功</a>     
  <br />  
  <a id="ajaxFail" href="#">用戶名爲空建立失敗</a>  
</body>  
</html>
相關文章
相關標籤/搜索