异常处理

一、Struts2 声明式异常处理

1.通过配置的方式捕获指定类型异常,由 ExceptionMappingInterceptor 拦截器将异常信息(ExceptionHolder:exceptionStack,exception)压入栈顶,然后通过 OGNL 表达式在页面中获取异常信息。

2.Action 级别,只对当前 Action 对应类型的异常起作用,在 struts2 配置文件 action 节点内部使用 exception-mapping 来映射异常。如:

<action name="arithmeticAction" class="com.nucsoft.struts2.helloworld.ExceptionAction" method="arithmeticAction">
    <exception-mapping exception="java.lang.ArithmeticException" result="arithmeticException"/>
    <result>/success.jsp</result>
    <result name="arithmeticException">/arithmeticException.jsp</result>
</action>

可以在错误页面获取异常信息:<s:property value="exception"/>

3.GLOBAL 级别,针对所有 Action 对应类型的异常都起作用

<global-results>
    <result name="globalException">/exception.jsp</result>
</global-results>

<global-exception-mappings>
    <exception-mapping exception="java.lang.Exception" result="globalException"/>
</global-exception-mappings>

页面中,可以通过 OGNL 表达式获取值栈中的异常信息。

注意:

(1)GLOBAL 级别的声明式异常配置必须配置在所有 action 之前,同时,global-results 节点需要配置在 global-exception-mappings 之前。

(2)当捕获到一个异常对象时,如果同时存在Action和全局两种级别的映射信息,则优先使用Action级别

未完,待续

原文地址:https://www.cnblogs.com/solverpeng/p/5652462.html