小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Struts2 異常處理

 CevenCheng 2010-09-25

來源:http://blog.sina.com.cn/s/blog_4b62e4a901008jlo.html
Struts2的異常處理機制:

任何成熟的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﹥元素的子元素配置。

Java代碼 
  1. package com.dingxun.exception;     
  2.     
  3. import java.sql.SQLException;     
  4. import com.opensymphony.xwork2.ActionSupport;     
  5.     
  6. public class Struts2Exception extends ActionSupport{     
  7.          
  8.     private static final long serialVersionUID = -87324321702425020L;     
  9.     private String username;     
  10.          
  11.     public String getUsername() {     
  12.         return username;     
  13.     }     
  14.     
  15.     public void setUsername(String username) {     
  16.         this.username = username;     
  17.     }     
  18.     
  19.          
  20.     public String execute() throws Exception{     
  21. //      if("logo".equals(getUsername())){     
  22. //          return SUCCESS;     
  23. //      }else{     
  24. //          throw new SQLException("用戶名不正確");     
  25. //      }     
  26.         throw  new java.lang.Exception("系統(tǒng)錯誤");     
  27.     }     
  28.     
  29. }    

 

 

Java代碼 
  1. <?xml version="1.0" encoding="UTF-8" ?>     
  2. <!DOCTYPE struts PUBLIC     
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
  4.     "http://struts./dtds/struts-2.0.dtd">     
  5.     
  6. <struts>     
  7.     <package name="aa" extends="struts-default">     
  8.         <global-exception-mappings>     
  9.         <exception-mapping result="all" exception="java.lang.Exception"></exception-mapping>     
  10.         </global-exception-mappings>     
  11.         <action name="execptionTest" class="com.dingxun.exception.Struts2Exception">     
  12.             <result name="success">/exception/result.jsp</result>     
  13.             <exception-mapping result="sql" exception="java.sql.SQLException"/>     
  14.             <exception-mapping result="null" exception="java.lang.NullPointerException"/>     
  15.             <result name="sql">/exception/sql.jsp</result>     
  16.             <result name="null">/exception/null.jsp</result>     
  17.             <result name="all">/exception/all.jsp</result>     
  18.         </action>     
  19.     </package>     
  20. </struts>    

 

 上面是個用戶登陸的action及配置,定義了兩個局部異常和一個全局異常
當輸入的用戶名和密碼不為callan和fjf時,會拋出SQLException異常,局部異常配置起作用,會定向到sql.jsp,如果注釋局部sql異常,全局異常起作用,定向到all.jsp

 

可以使用Struts2的標簽來輸出異常信息
<s:property value="exception.message"/>
<s:property value="exceptionStack"/>

例如sql.jsp頁面

<body>
   <s:property value="exception.message"/>
 </body>
可以輸出 用戶名密碼不正確

 


本文來自CSDN博客,轉(zhuǎn)載請標明出處:http://blog.csdn.net/thunder_1985/archive/2009/09/14/4550618.aspx

 

 

 

 

   輸出異常信息:

使用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做異常處理還是比較方便的了

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多