• 异常处理


    一、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级别

    未完,待续

  • 相关阅读:
    六白话经典算法系列 高速分拣 高速GET
    neu1458 方格取数 dp解法
    自然语言处理---新词发现---微博数据预处理2
    JQuery之初探
    TFS(Team Foundation Server)介绍和入门
    ZooKeeper的学习与应用
    软考之路(六)---数据库---深入浅出 三层模式两级映像
    Open the Lock
    C/C++产生随机数
    RPM安装包-Spec文件參数具体解释与演示样例分析
  • 原文地址:https://www.cnblogs.com/solverpeng/p/5652462.html
Copyright © 2020-2023  润新知