Struts2的異常處理機制

2021-08-31 17:13:03 字數 1997 閱讀 7743

struts2的異常處理機制:

任何成熟的mvc框架都應該提供成就的異常處理機制。strut2也不例外。struts2提供了一種宣告式的異常處理方式。struts2也是通過配置的***來實現異常處理機制的。

exception:此屬性指定該異常對映所設定的異常型別。

result:此屬性指定action出現該異常時,系統轉入result屬性所指向的結果。

異常對映也分為兩種:

輸出異常資訊:

使用struts2的標籤來輸出異常資訊:

﹤s:property value="exception.message"/﹥:輸出異常物件本身。

﹤s:property value="exceptionstack"/﹥: 輸出異常堆疊資訊。

利用struts2的異常處理機制和***機制可以很方便的實現異常處理功能,你不再需要在action中捕獲異常,並丟擲相關的異常了,這些都交給***來幫你做了。

1. 在 struts.xml 檔案中,宣告全域性異常對映,以及對應的全域性異常**如下所示:

﹤global-results﹥

﹤result name="error"﹥/admin/error/errdisplay.ftl﹤/result﹥

﹤/global-results﹥

busines***ception 是異常處理類,**如下所示:

public class busines***ception extends runtimeexception

public busines***ception(throwable throwable)

public busines***ception(throwable throwable, string frdmessage)

private static string createfriendlyerrmsg(string msgbody)

}2. /admin/error/errdisplay.ftl 頁面

這個頁面很簡單:

﹤body﹥

﹤h2﹥

出現異常啦

﹤/h2﹥

﹤hr/﹥

﹤h3 style="color:red"﹥

﹤!-- 獲得異常物件 --﹥

$﹤/h3﹥

﹤br/﹥

﹤!-- 異常堆疊資訊(開發人員用) --﹥

﹤div style="display:none;"﹥

$﹤/div﹥

﹤/body﹥

﹤body﹥

﹤h2﹥

出現異常啦

﹤/h2﹥

﹤hr/﹥

﹤h3 style="color:red"﹥

﹤!-- 獲得異常物件 --﹥

$﹤/h3﹥

﹤br/﹥

﹤!-- 異常堆疊資訊(開發人員用) --﹥

﹤div style="display:none;"﹥

$﹤/div﹥

﹤/body﹥

public string intercept(actioninvocation invocation) throws exception

catch(dataacces***ception ex)catch(nullpointerexception ex)catch(ioexception ex)catch(classnotfoundexception ex)catch(arithmeticexception ex)catch(arrayindexoutofbound***ception ex)catch(illegalargumentexception ex)catch(classcastexception ex)catch(securityexception ex)catch(sqlexception ex)catch(nosuchmethoderror ex)catch(internalerror ex)catch(exception ex)

after(invocation, result);

return result;

}struts2做異常處理還是比較方便的了。

Struts2的異常處理機制

struts2採用宣告式的方法管理異常處理,因此我們無需在execute方法體內寫大量的try.catch.語句來捕獲異常,execute方法將產生的所有異常丟擲,統一交由struts2框架處理,我們只需在struts.xml檔案中配置異常的對映機制,struts2便能夠處理並轉入相應的檢視資源。異...

Struts2的異常處理機制

struts2的異常處理機制 關鍵字 struts2的異常處理機制 struts2的異常處理機制 任何成熟的mvc框架都應該提供成就的異常處理機制。strut2也不例外。struts2提供了一種宣告式的異常處理方式。struts2也是通過配置的 來實現異常處理機制的。exception 此屬性指定該...

Struts2的異常處理機制

struts2的異常處理機制 任何成熟的mvc框架都應該提供成就的異常處理機制。strut2也不例外。struts2提供了一種宣告式的異常處理方式。struts2也是通過配置的 來實現異常處理機制的。exception 此屬性指定該異常對映所設定的異常型別。result 此屬性指定action出現該...