| 來源:http://blog.sina.com.cn/s/blog_4b62e4a901008jlo.html 任何成熟的MVC框架都應(yīng)該提供成就的異常處理機制。Strut2也不例外。Struts2提供了一種聲明式的異常處理方式。Struts2也是通過配置的攔截器來實現(xiàn)異常處理機制的。 Struts2的異常處理機制通過在struts.xml文件中配置﹤exception-mapping …﹥元素完成的,配置該元素時,需要指定兩個屬性: exception:此屬性指定該異常映射所設(shè)置的異常類型。 result:此屬性指定Action出現(xiàn)該異常時,系統(tǒng)轉(zhuǎn)入result屬性所指向的結(jié)果。 
 局部異常映射:﹤exception-mapping…﹥元素作為﹤action…﹥元素的子元素配置。 全局異常映射:﹤exception-mapping…﹥元素作為﹤global-exception-mappings﹥元素的子元素配置。 
 
 
 
 
  上面是個用戶登陸的action及配置,定義了兩個局部異常和一個全局異常 
 可以使用Struts2的標簽來輸出異常信息 例如sql.jsp頁面 <body> 
 
 
 
 
 
 輸出異常信息: 使用Struts2的標簽來輸出異常信息: ﹤s:property value="exception.message"/﹥:輸出異常對象本身。 ﹤s:property value="exceptionStack"/﹥: 輸出異常堆棧信息。 
 
 
 利用struts2的異常處理機制和攔截器機制可以很方便的實現(xiàn)異常處理功能,你不再需要在Action中捕獲異常,并拋出相關(guān)的異常了,這些都交給攔截器來幫你做了。 
 
 1. 在 struts.xml 文件中,聲明全局異常映射,以及對應(yīng)的全局異常轉(zhuǎn)發(fā)如下所示: ﹤global-results﹥ ﹤result name="error"﹥/admin/error/ErrDisplay.ftl﹤/result﹥ ﹤/global-results﹥ ﹤global-exception-mappings﹥ ﹤exception-mapping result="error" exception="com.orizone.hbmobile.hbcm.struts.BusinessException"﹥﹤/exception-mapping﹥ ﹤/global-exception-mappings﹥ 
 BusinessException 是異常處理類,代碼如下所示: 
 public class BusinessException extends RuntimeException { 
 private static final long serialVersionUID = 0xc1a865c45ffdc5f9L; 
 public BusinessException(String frdMessage) { super(createFriendlyErrMsg(frdMessage)); 
 } 
 public BusinessException(Throwable throwable) { super(throwable); } 
 public BusinessException(Throwable throwable, String frdMessage) { super(throwable); 
 } 
 
 private static String createFriendlyErrMsg(String msgBody){ String prefixStr = "抱歉,"; String suffixStr = " 請稍后再試或與管理員聯(lián)系!"; 
 StringBuffer friendlyErrMsg = new StringBuffer(""); 
 friendlyErrMsg.append(prefixStr); friendlyErrMsg.append(msgBody); friendlyErrMsg.append(suffixStr); 
 return friendlyErrMsg.toString(); } } 
 
 2. /admin/error/ErrDisplay.ftl 頁面 這個頁面很簡單: ﹤body﹥ ﹤h2﹥ 出現(xiàn)異常啦 ﹤/h2﹥ ﹤hr/﹥ ﹤h3 style="color:red"﹥ ﹤!-- 獲得異常對象 --﹥ 
 ${exception.message?default("")} ﹤/h3﹥ ﹤br/﹥ ﹤!-- 異常堆棧信息(開發(fā)人員用) --﹥ ﹤div style="display:none;"﹥ ${exceptionStack?default("")} ﹤/div﹥ ﹤/body﹥ ﹤body﹥ ﹤h2﹥ 出現(xiàn)異常啦 ﹤/h2﹥ ﹤hr/﹥ ﹤h3 style="color:red"﹥ ﹤!-- 獲得異常對象 --﹥ 
 ${exception.message?default("")} ﹤/h3﹥ ﹤br/﹥ ﹤!-- 異常堆棧信息(開發(fā)人員用) --﹥ ﹤div style="display:none;"﹥ ${exceptionStack?default("")} ﹤/div﹥ ﹤/body﹥ 
 3. 在攔截器中,捕獲常見的異常,并以友好異常信息拋出,相關(guān)代碼如下所示: public String intercept(ActionInvocation invocation) throws Exception { before(invocation); String result = ""; 
 try{ result = invocation.invoke(); }catch(DataAccessException ex){ throw new BusinessException("數(shù)據(jù)庫操作失??!"); }catch(NullPointerException ex){ throw new BusinessException("調(diào)用了未經(jīng)初始化的對象或者是不存在的對象!"); }catch(IOException ex){ throw new BusinessException("IO異常!"); }catch(ClassNotFoundException ex){ throw new BusinessException("指定的類不存在!"); }catch(ArithmeticException ex){ throw new BusinessException("數(shù)學運算異常!"); }catch(ArrayIndexOutOfBoundsException ex){ throw new BusinessException("數(shù)組下標越界!"); }catch(IllegalArgumentException ex){ throw new BusinessException("方法的參數(shù)錯誤!"); }catch(ClassCastException ex){ throw new BusinessException("類型強制轉(zhuǎn)換錯誤!"); }catch(SecurityException ex){ throw new BusinessException("違背安全原則異常!"); }catch(SQLException ex){ throw new BusinessException("操作數(shù)據(jù)庫異常!"); }catch(NoSuchMethodError ex){ throw new BusinessException("方法末找到異常!"); }catch(InternalError ex){ throw new BusinessException("Java虛擬機發(fā)生了內(nèi)部錯誤"); }catch(Exception ex){ throw new BusinessException("程序內(nèi)部錯誤,操作失?。?); } 
 after(invocation, result); return result; } struts2做異常處理還是比較方便的了 | 
|  | 
來自: CevenCheng > 《異常處理》