Struts2异常处理

Struts2提供了一种声明式的异常处理方式

1、在Struts.xml中配置<exception-mapping .../>,包含两个属性:
exception:此属性指定该异常映射所设置的异常类型
result:此属性指定所指向的结果

2、异常映射分为两种:
局部异常映射:<exception-mapping .../>作为<action .../>子元素
全局异常映射:<exception-mapping .../>作为<global-exception-mappings>子元素


3、重写execute()方法时要抛出所有异常类型
public String execute() throws Exception{}

4、配置文件
<package name="gao" extends="struts-defeault">
<global-results>
<result name="sql">/exception.jsp</result>
<result name="root">/exception.jsp</result>
</global-results>
<!--定义全局异常映射-->
<global-exception-mappings>
<!--Action抛出SQLException异常时,转入名为sql的结果-->
<exception-mapping exception="java.sql.SQLException" result="sql">
<!--Action抛出Exception异常时,转入名为root的结果-->
<exception-mapping exception="java.lang.Exception" result="root">
</global-exception-mappings>
<action name="login" class="gao.LoginAction">
<exception-mapping exception="gao.MyException" result="my" />
<result name="my">/exception.jsp</result>
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>

5、输出异常信息
<s:property value="exception"/>输出异常对象本身
<s:property value="exception.message"/>
<s:property value="exceptionStack"/>输出异常堆栈信息

原文地址:https://www.cnblogs.com/ikuman/p/2244591.html