struts2配置之全局result及異常處理

提起異常處理,不由想起鋪天蓋地的try{...}catch{...}語句,在方法中使用catch塊處理異常已經成爲我們的習慣。

在struts2中,是該改變一下的時候了;

struts2憑藉自己的強大攔截器功能,隆重推出聲明式異常處理機制。

struts2的聲明式異常處理機制是在struts.xml文件中配置<exception-mapping>來實現的

不廢話,看代碼:

<package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="default-error" />
		<global-results>
			<result name="login" type="redirect">/admin/login.jsp</result>
			<result name="index" type="redirect">/admin/index.jsp</result>
			<result name="error">/admin/500.html</result>
		</global-results>
		<global-exception-mappings>
			<exception-mapping result="error" exception="java.lang.Exception" />
		</global-exception-mappings>
		<action name="default-error">
			<result>/admin/500.html</result>
		</action>
</package>

從這可以看出action中execute方法throw exception的良苦用心了,將異常信息直接拋給struts攔截器處理。

如果想顯示異常信息,在前臺頁面中使用

<s:property value="exception.message"/>

來獲取異常信息,用

<s:property value="exceptionStack"/>
來獲取異常的堆棧信息。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章