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>