前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >(17)Struts2_异常处理: exception-mapping 元素

(17)Struts2_异常处理: exception-mapping 元素

作者头像
qubianzhong
发布2018-09-19 13:34:36
4890
发布2018-09-19 13:34:36
举报
文章被收录于专栏:行者常至

异常处理: exception-mapping 元素

在action方法中添加
代码语言:javascript
复制
        int i=1/0;
请求action后,结果为:
这里写图片描述
这里写图片描述

在struts.xml中添加异常处理:exception-mapping元素
代码语言:javascript
复制
        <action name="czy_save" class="com.qbz.struts2_02.GG_CZY" method="save">
            <exception-mapping result="ArithmeticException" exception="java.lang.ArithmeticException"></exception-mapping>
            <result name="ArithmeticException">/WEB-INF/page/Input.jsp</result>

            <result name="save">/WEB-INF/page/Show.jsp</result>
        </action>
此时,页面将直接跳转到Input.jsp:
这里写图片描述
这里写图片描述
在Input.jsp加入标签显示exception信息:
代码语言:javascript
复制
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  <form action="czy_save.action" method="post">
      <s:debug></s:debug><br>
      <s:property value="exception"/><br>
      <s:property value="exception.message"/><br>
       编号:<input type="text" name="dlh"/><br>
       姓名:<input type="text" name="name"/><br>
       部门:<input type="text" name="bmmc"/><br>
       <input type="submit" value="保存"/>
   </form>
  </body>
</html>
请求action后的页面效果:
这里写图片描述
这里写图片描述
点击 Debug :
这里写图片描述
这里写图片描述
可见,对象栈的 栈顶 是异常对象,所以在上面页面没用下标就直接读取属性。ExceptionHolder有两个属性,一个是exceptionStack,一个是exception。

我们来看,Struts2是如何做到异常映射处理的。
在struts_default.xml中查看:
我们的package的父类struts-default中引用的默认拦截器
代码语言:javascript
复制
<default-interceptor-ref name="defaultStack"/>
这里写图片描述
这里写图片描述

这里写图片描述
这里写图片描述
ctrl+shift+t 查看 ExceptionMappingInterceptor 源码
代码语言:javascript
复制
public class ExceptionMappingInterceptor extends AbstractInterceptor {

    //省略......

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        String result;

        try {
            result = invocation.invoke();
        } catch (Exception e) {
            if (isLogEnabled()) {
                handleLogging(e);
            }
            List<ExceptionMappingConfig> exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings();
            ExceptionMappingConfig mappingConfig = this.findMappingFromExceptions(exceptionMappings, e);
            if (mappingConfig != null && mappingConfig.getResult()!=null) {
                Map parameterMap = mappingConfig.getParams();
                // create a mutable HashMap since some interceptors will remove parameters, and parameterMap is immutable
                invocation.getInvocationContext().setParameters(new HashMap<String, Object>(parameterMap));
                result = mappingConfig.getResult();
                publishException(invocation, new ExceptionHolder(e));
            } else {
                throw e;
            }
        }

        return result;
    }

    //省略......

    /**
     * Default implementation to handle ExceptionHolder publishing. Pushes given ExceptionHolder on the stack.
     * Subclasses may override this to customize publishing.
     *
     * @param invocation The invocation to publish Exception for.
     * @param exceptionHolder The exceptionHolder wrapping the Exception to publish.
     */
    protected void publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder) {
        invocation.getStack().push(exceptionHolder);
    }

}
可见,当catch到异常之后publishException(invocation, new ExceptionHolder(e))把异常压入了栈中。

每个action是不是都要配置异常处理呢?当然不用,下面来看global-exception-mappingsglobal-results
在struts.xml中配置如下:
代码语言:javascript
复制
        <global-results>
            <result name="ArithmeticException">/WEB-INF/page/Input.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping result="ArithmeticException" exception="java.lang.ArithmeticException"></exception-mapping>
        </global-exception-mappings>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年09月04日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 异常处理: exception-mapping 元素
    • 在action方法中添加
      • 请求action后,结果为:
        • 在struts.xml中添加异常处理:exception-mapping元素
          • 此时,页面将直接跳转到Input.jsp:
            • 在Input.jsp加入标签显示exception信息:
              • 请求action后的页面效果:
                • 点击 Debug :
                  • 可见,对象栈的 栈顶 是异常对象,所以在上面页面没用下标就直接读取属性。ExceptionHolder有两个属性,一个是exceptionStack,一个是exception。
                    • 我们来看,Struts2是如何做到异常映射处理的。
                      • 在struts_default.xml中查看:
                        • 我们的package的父类struts-default中引用的默认拦截器
                          • ctrl+shift+t 查看 ExceptionMappingInterceptor 源码
                            • 可见,当catch到异常之后publishException(invocation, new ExceptionHolder(e))把异常压入了栈中。
                              • 每个action是不是都要配置异常处理呢?当然不用,下面来看global-exception-mappings、global-results 。
                                • 在struts.xml中配置如下:
                                领券
                                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档