首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

[转]Struts2的错误处理机制

2012-10-31 
[转]Struts2的异常处理机制Struts2采用声明式的方法管理异常处理,因此我们无需在execute方法体内写大量的t

[转]Struts2的异常处理机制
Struts2采用声明式的方法管理异常处理,因此我们无需在execute方法体内写大量的try...catch...语句来捕获异常,execute方法将产生的所有异常抛出,统一交由Struts2框架处理,我们只需在struts.xml文件中配置异常的映射机制,Struts2便能够处理并转入相应的视图资源。

异常映射可分为全局异常映射和局部异常映射,它和<result .../>的性质是一样的。

配置代码如下:



Xml代码
<struts> 
    <package name="struts2" extends="struts-default"> 
        <!-- 配置全局异常映射 --> 
        <global-exception-mappings> 
            <exception-mapping result="sql" exception="java.sql.SQLException"/> 
        </global-exception-mappings> 
        <action name="login" exception="com.test.exception.MyException"/> 
            <result name="success">result.jsp</result> 
            <result name="sql">error.jsp</result> 
            <result name="myException">error.jsp</result> 
        </action> 
    </package> 
</struts> 

<struts>
<package name="struts2" extends="struts-default">
<!-- 配置全局异常映射 -->
<global-exception-mappings>
<exception-mapping result="sql" exception="java.sql.SQLException"/>
</global-exception-mappings>
<action name="login" exception="com.test.exception.MyException"/>
<result name="success">result.jsp</result>
<result name="sql">error.jsp</result>
<result name="myException">error.jsp</result>
</action>
</package>
</struts>

为了在异常处理页面中显示异常信息,我们可以使用<s:property/>标签来输出异常信息:



<s:property value="exception"/>:输出异常信息本身

<s:property value="exceptionStack"/>:输出异常堆栈信息

热点排行