异常处理: exception-mapping 元素

在action方法中添加

        int i=1/0;

请求action后,结果为:


在struts.xml中添加异常处理:exception-mapping元素

        <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信息:

<%@ 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中引用的默认拦截器

<default-interceptor-ref name="defaultStack"/>

ctrl+shift+t 查看 ExceptionMappingInterceptor 源码

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-mappings、global-results 。

在struts.xml中配置如下:

        <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>
版权声明:本文为博主原创文章,允许转载,请标明出处。 https://blog.csdn.net/qwdafedv/article/details/52431038

Struts2 - 异常处理: exception-mapping 元素的更多相关文章

  1. 16.怎样自学Struts2之Struts2异常处理[视频]

    16.怎样自学Struts2之Struts2异常处理[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了,仅仅好传到百度云上: http://pa ...

  2. Struts2中访问web元素的四种方式

    Struts2中访问web元素的四种方式如下: 通过ActionContext来访问Map类型的request.session.application对象. 通过实现RequestAware.Sess ...

  3. 异常处理—Exception(三)

    最近有点事,把这个系列给落下了,给大家道个歉,这里还要感谢我的老婆,谢谢她一直对我的支持:) 系列回顾: 1.异常处理--Exception(一) 2.异常处理—Exception(二) 上一篇中主要 ...

  4. 异常处理—Exception(二)

    在上一篇中"异常处理--Exception(一)"中,跟大家简单介绍了一下Exception,也使大家充分的了解了Exception管理在一个项目中的重要性,那如何在我们的项目中处 ...

  5. Struts(十二):异常处理:exception-mapping元素

    配置当前action的声明异常处理 1.exception-mapping元素中有2个属性 exception:指定需要捕获的异常类型 result:指定一个响应结果,该结果将在捕获到异常时被执行.即 ...

  6. struts2异常处理机制

    一.处理一般异常(javaBean异常) struts2进行异常处理首先需要添加exception拦截器,而默认拦截器栈已经加入了这个拦截器,所以不用特意的声明.在Struts 2框架中,采用声明式异 ...

  7. struts2 异常处理3板斧

    板斧1:找不到action的错误 在struts.xml中参考如下配置 <struts> ... <package name="default" namespac ...

  8. struts2异常处理及类型转换

    一.struts2对异常的处理 1.自定义局部异常: <action> <exception-mapping result="sonException" exce ...

  9. Akka(26): Stream:异常处理-Exception handling

    akka-stream是基于Actor模式的,所以也继承了Actor模式的“坚韧性(resilient)”特点,在任何异常情况下都有某种整体统一的异常处理策略和具体实施方式.在akka-stream的 ...

随机推荐

  1. shader随记

    o.WorldPos = normalize(mul((float4x4)unity_ObjectToWorld, v.vertex)).xyz;

  2. Stacks of Flapjacks(栈)

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

  3. 九度OJ 1351:数组中只出现一次的数字 (位运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3098 解决:906 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 输入: 每个 ...

  4. spring boot的对象注入

    1 需求 现在我们的项目中需要引入一个java类库,我想要很方便的使用该类库中的一个类,并且我想要创建这个类的一个单例对象.然后可以很方便的在各个模块中用@AutoWired进行对象注入. 比如一个配 ...

  5. Bootstrap学习-网格系统

    1.实现原理 网格系统的实现原理非常简单,仅仅是通过定义容器大小,平分12份(也有平分成24份或32份,但12份是最常见的),再调整内外边距,最后结合媒体查询,就制作出了强大的响应式网格系统.Boot ...

  6. WiX 中XML引用变量说明

    WiX 安装工程中的XML 文件所引用变量说明: The WiX project supports the following project reference variables: Variabl ...

  7. unix网络编程笔记(二)

    第四章笔记 1. 基本Tcpclient/server程序的套接字函数 2. socket函数: int socket(int family,int type,int protocol); (1)so ...

  8. 批处理设置IP地址

    echo offecho 修改[本地连接]IP......netsh interface IP set address "本地连接" static 138.8.8.111 255. ...

  9. 运行“cordova build android” - 无法找到属性android:fontVariationSettings和android:ttcIndex

    :app:processArm64DebugResourcesC:\Users\xfcao\.gradle\caches\transforms-1\files-1.1\xwalk_core_libra ...

  10. python3 批量缩放图片为iphone5的640*1136以下

    try: from PIL import Image, ImageDraw, ImageFont, ImageEnhance except ImportError: import Image, Ima ...