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.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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值