Struts2声明式异常处理

Struts2支持声明式异常处理,不需要每个exception都捕获

方法只需throws Exception 即可

xml中配置如下

1、全局异常

<!-- 全局的配置,该pacakage中所有action异常都会跳转到/error.jsp -->
     <global-results>
            <result name="error">/error.jsp</result>
        </global-results>
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

2、局部异常

<!-- 局部异常 ,只在该action下有效,父节点为<action></action> -->
<result name="error">/error.jsp</result>
        <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>

 

 

error.jsp页面中插入<s:debug></s:debug>标签

可观察到 值栈栈顶对象为ExceptionHolder,jsp页面可通过如下代码获取错误信息

<s:debug></s:debug>
<s:property value="exception.message"/><!-- 错误信息 -->
<br>
<s:property value="exceptionStack"/>

 

exception拦截器位于默认拦截器栈defaultStack的栈顶

所有Action请求先要被exception拦截 所以 栈里面的所有拦截器抛出异常最终都会被exception拦截器拦截

posted on 2015-02-21 22:00  刘溜溜  阅读(324)  评论(0编辑  收藏  举报

导航