Struts2异常处理机制

来源:互联网 发布:爱奇艺视频软件 编辑:程序博客网 时间:2024/05/17 08:14

1) 配置

使用Struts2时,在配置文件中配置异常映射就能拦截特定的异常,转入对应的页面。

<packagename="exetionLearn"extends="struts-default"namespace="/exception">

    <global-results>

       <resultname="showException">/WEB-INF/page/exceptionLearn/register.jsp</result>

    </global-results>

    <global-exception-mappings>

       <exception-mappingresult="showException"exception="java.lang.Exception"></exception-mapping>

    </global-exception-mappings>

    <actionname="register_*"class="com.gathershell.action.exception.RegisterAction"method="{1}">

       <resultname="showException">/WEB-INF/page/exceptionLearn/register.jsp</result>

            <resultname="success">/WEB-INF/page/success.jsp</result>

            <exception-mappingresult="showException"exception="java.sql.SQLException"></exception-mapping>

       </action>

    </package>

上面的全局异常映射作用于整个pakage中所有Action,而在某个ation下配置的异常映射只作用本Action,并且它的优先级高,若它能匹配,就不再去用全局异常映射。

2) 页面

<s:propertyvalue="exception.message"/>

${exception.message }<br/>

${exceptionStack }

在页面上可用Struts2的标签或EL表达式输出异常信息。输出异常对象用exception.message,输出异常堆栈用exceptionStack。

原创粉丝点击