Spring MVC 系統異常處理方式及性能對比
大部分公司所用的Spring框架版本是3.1版本以下,所以今天暫時總結3.1版本的Spring-MVC異常處理方式。
一、Spring MVC處理異常有3種方式:
(1)使用Spring-MVC提供的SimpleMappingExceptionResolver;
(2)實現Spring的異常處理接口HandlerExceptionResolver 自定義自己的異常處理器;
(3)使用@ExceptionHandler注解實現異常處理;
二、分別介紹這三種異常處理的實現方式:
(1)使用SimpleMappingExceptionResolver實現異常處理
只需要在Spring的配置文件applicationContext.xml中增加以下內容:
<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 接口自定義異常處理器
首先增加HandlerExceptionResolver 接口的實現類MyExceptionHandler,代碼如下:
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中增加以下內容:
<bean id="exceptionHandler" class="com.core.exception.MyExceptionHandler"/>
(3)使用@ExceptionHandler注解實現異常處理
首先要增加BaseController類,并在類中使用@ExceptionHandler注解聲明異常處理,代碼如下:
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
三、未捕獲異常的處理 :
對于Unchecked Exception而言,由于代碼不強制捕獲,往往被忽略,如果運行期產生了UncheckedException,而代碼中又沒有進行相應的捕獲和處理,則我們可能不得不面對尷尬的404、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> <!-- 可繼續增加服務器錯誤號的處理及對應顯示頁面 -->
四、比較異常處理方式的優缺點:
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層異常");
}
}</pre>
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());
}
}</pre>
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>
來自:http://my.oschina.net/CandyDesire/blog/333340