struts聲明式異常
Struts框架允許程序將異常拋給Struts框架。Struts框架可以處理拋出來的異常。
局部異常:exception-mapping是定義在action中的。action中所有的方法,都會共享exception-mapping的配置。
<action name="exceptionAction"class="com.action.ExceptionAction">
<exception-mapping exception="java.lang.ArithmeticException"
result="error_arith"></exception-mapping>
<exception-mapping exception="java.io.FileNotFoundException"
result="error_notfound"></exception-mapping>
<exception-mapping exception="java.lang.Exception"
result="error_exception"></exception-mapping>
<result name="succ">/succ_exception.jsp</result>
<result name="error_arith">/error_arith.jsp</result>
<result name="error_notfound">/error_notfound.jsp</result>
</action>
全局異常:定義在package,package中所有的action都可以共享全局聲明式異常的配置。
<package name="default"namespace="/" extends="struts-default">
<!-- 全局的異常配置 -->
<global-results>
<result name="error_arith">/error_arith.jsp</result>
<result name="error_notfound">/error_notfound.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.ArithmeticException"
result="error_arith"></exception-mapping>
<exception-mapping exception="java.io.FileNotFoundException"
result="error_notfound"></exception-mapping>
<exception-mapping exception="java.lang.Exception"
result="error_exception"></exception-mapping>
</global-exception-mappings>
</package>
不同package之間的處理:通過package之間繼承的方式,共享聲明式異常的配置。
<package name="other"namespace="/other" extends="default">
<action name="exceptionOtherAction" class="com.action.ExceptionOtherAction"></action>
</package>
Struts的異常處理機制與javaEE中處理的異常相似的原理。
web.xml中配置:
<error-page>
<error-code>404</error-code>
<location>/xxx.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/yyy.jsp</location>
</error-page>