struts声明式异常

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.jspresult>

         <result name="error_arith">/error_arith.jspresult>

         <result name="error_notfound">/error_notfound.jspresult>

      action>

全局异常:定义在package,package中所有的action都可以共享全局声明式异常的配置。

   <package name="default"namespace="/" extends="struts-default">

     

      <global-results>

         <result name="error_arith">/error_arith.jspresult>

         <result name="error_notfound">/error_notfound.jspresult>

      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>404error-code>

         <location>/xxx.jsplocation>

      error-page>

     

      <error-page>

         <error-code>500error-code>

         <location>/yyy.jsplocation>

      error-page >

你可能感兴趣的:(struts2)