struts2 异常处理

 

异常处理后,能够提供友好的用户界面,而不是一些错误的代码信息.

利用struts2的异常处理机制可以很方便的实现异常处理,你不再需要在Action中捕获异常,并抛出相关的异常了,这些都交给拦截器来帮你做了。在struts-default.xml中己经设置了拦截器,因此我们可以直接使用声明式异常.struts2-default-xml声明的拦截器如下:

  1. <interceptors>  
  2. <interceptor name="exception"    
  3. class="com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor"/>  
  4. </interceptors>  

 

异常的类型可以分为两种:局部异常映射和全局异常映射

 

当Action抛出异常时,会在局部中和全局中查找与之相匹配的异常,如果局部和全局中都有满足的异常映射,以局部的为准.

 

可以使用Struts2的标签来输出异常信息
<s:property value="exception.message"/>
<s:property value="exceptionStack"/>

 

 <global-results>
   <result name="exceptionPage">/pages/fail.jsp</result>
  </global-results>
  <global-exception-mappings>
   <exception-mapping result="exceptionPage" exception="java.lang.Exception" />
  </global-exception-mappings>

 

<interceptor-ref name="exception"></interceptor-ref>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值