springMVC相關—異常處理,springmvc異常處理
一、基於@ExceptionHandler 註解的異常處理方法:
1.加上<mvc:annotation-driven>標籤:(該標籤是標配,開發時一般都攜帶)
2.在當前Handler中定義由@ExceptionHandler註解修飾的方法,用於處理異常資訊!
@ExceptionHandler(value={異常類型.class})
@ExceptionHandler(value={異常類型.class,異常類型.class,........})
注意:
1.@ExceptionHandler方法修飾的入參中可以加入Exception類型的參數,該參數即對應發生的異常資訊
1 @ExceptionHandler(value={ArithmeticException.class})2 public String ExcptionHandler(Exception ex) {3 System.out.println(ex);4 return "error";5 }6 //Exception ex作為入參,可以顯示異常對象資訊到控制台樣本
2.@ExceptionHandler方法的入參中不能傳入Map.若希望把異常資訊傳到頁面上,需要使用ModelAndView作為方法的傳回值。(也不可以用model)
1 類中: 2 @ExceptionHandler(value={ArithmeticException.class}) 3 public ModelAndView ExcptionHandler(Exception ex) { 4 ModelAndView mv=new ModelAndView(); 5 mv.setViewName("error"); 6 mv.addObject("exception", ex); 7 return mv; 8 } 9 錯誤頁面:10 <h1>這是一個錯誤頁面!</h1>11 ${exception }樣本
3.@ExceptionHandler 註解定義的方法優先順序問題:
例如發 生的是NullPointerException,但是聲明的異常有 RuntimeException 和 Exception,此候會根據異常的最近 繼承關係找到繼承深度最淺的那個 @ExceptionHandler 註解方法,即標記了 RuntimeException 的方法
1 @ExceptionHandler(value={Exception.class}) 2 public void ExcptionHandler1() { 3 System.out.println("發生異常"); 4 5 } 6 7 @ExceptionHandler(value={ArithmeticException.class}) 8 public String ExcptionHandler() { 9 System.out.println("發生異常");10 return "error";11 }12 13 @RequestMapping(value="/testException")14 public String testException(@RequestParam(value="i") Integer i) {15 System.out.println(10/i);16 return "success";17 } 18 //註解上的運行異常類型與實際的異常類型比較,運行最準確的哪個(範圍小,最貼近)執行異常處理 @ExceptionHandler註解多異常方法處理
4.ExceptionHandlerMethodResolver 內部若找不 到@ExceptionHandler 註解的話,會找@ControllerAdvice 中的@ExceptionHandler 註解方法
對所有的handler異常資訊處理:
1 @ControllerAdvice 2 public class HandlerException { 3 4 @ExceptionHandler(value={ArithmeticException.class}) 5 public String handleException() { 6 7 return "error"; 8 } 9 }10 //所有Handler的ArithmeticException異常均會經過上面函數來進行統一處理全域異常處理
注意:
在程式執行處理類中有異常處理方法的話,遇到異常程式會優先走本類中的異常處理方法,
若是沒有的話,則會找@ControllerAdvice 註解類中的@ExceptionHandler 註解方法。
1、@ControllerAdvice 註解類:處理全域異常。
2、異常處理:總是優先走本類中方法。
1 jsp頁面中: 2 <a href="${pageContext.request.contextPath }/testException?i=2">異常處理</a> 3 同一類中: 4 @ExceptionHandler(value={ArithmeticException.class}) 5 public String ExcptionHandler() { 6 System.out.println("發生異常"); 7 return "error"; 8 } 9 10 @RequestMapping(value="/testException")11 public String testException(@RequestParam(value="i") Integer i) {12 System.out.println(10/i);13 return "success";14 } 15 //將網址上i =2改為i =0時發生異常,通過註解的方法轉到error頁面! @ExceptionHandler異常處理基礎樣本
二、基於配置的異常處理:
如果希望對所有異常進行統一處理,可以使用 SimpleMappingExceptionResolver,它將異常類名映射為 視圖名,即發生異常時使用對應的視圖報告異常
1 <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 2 <!-- 指定在在request域中擷取異常資訊所需要的key:即ex --> 3 <property name="exceptionAttribute" value="ex"></property> 4 <!-- 指定異常映射 --> 5 <property name="exceptionMappings"> 6 <!-- property常用語鍵和值均為字串類型 --> 7 <props> 8 <!-- 由prop標籤的key屬性指定發生異常的全類名,由值指定出現異常去哪個頁面! --> 9 <prop key="java.lang.ArithmeticException">error</prop>10 </props>11 </property>12 </bean>
異常處理配置資訊