• 配置当前action的声明异常处理

1、exception-mapping元素中有2个属性
  exception:指定需要捕获的异常类型
  result:指定一个响应结果,该结果将在捕获到异常时被执行。即可以来自当前action的声明,也可以来自global-results声明。

2、可以在视图上通过<s:property>标签显示异常消息。

基于上几个章节的例子开始往下写,修改save函数:

    public String save(){
System.out.println("save"); int val=10/0; return "success";
}

此时访问http://localhost:8080/Struts_01/,输入信息点击“提交”按钮跳转到details.jsp,当访问save函数时,就跑出了异常信息:

此时,我们发现异常信息并没有被合理的处理掉,比如出错了,我们让页面跳转到error.jsp该多好,其实struts2中通过配置struts.xml是可以实现的。

修改struts.xml:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!--
<constant name="struts.action.extension" value="action" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
-->
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default">
<action name="product-save" class="com.dx.struts2.valuestack.Product" method="save">
<exception-mapping result="error" exception="java.lang.ArithmeticException"></exception-mapping>
<result name="error">/error.jsp</result>
<result>/details.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
 

添加在WebContent下添加error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Insert title here</title>
</head>
<body>
  error
</body>
</html>

重启服务,重新访问页面,并提交参数,发现页面自动跳转到了error.jsp:

如果光跳转过来不太完美,如果可以显示错误信息不是更好,实际上通过EL或者s:property标签是可以实现的,修改error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
error<br/>
<s:debug/>
<s:property value="exception" /><br/>
${exception}<br/>
${exception.message}<br/>
<s:property value="exception.message"/><br/>
<s:property value="exceptionStack"/><br/>
${exceptionStack} </body>
</html>

显示信息:
s:debug第一个元素类型为:com.opensymphony.xwork2.interceptor.ExceptionHolder

元素包含两个属性:exception和exceptionStack

  • 可以通过global-exception-mappings元素为应用程序提供一个全局性的异常捕获映射。

  1、但在global-exception-mappings元素下声明任何exception-mapping元素只能引用在global-results元素下声明的某个result元素。

只需要修改struts.xml即可,其他不需要变动:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!--
<constant name="struts.action.extension" value="action" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
-->
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default">
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.ArithmeticException"></exception-mapping>
</global-exception-mappings>
<action name="product-save" class="com.dx.struts2.valuestack.Product" method="save">
<!--
<exception-mapping result="error" exception="java.lang.ArithmeticException"></exception-mapping>
<result name="error">/error.jsp</result>
-->
<result>/details.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>

重新访问即可,其他内容不影响显示。

  • 声明式异常处理机制由ExceptionMappingInterceptor拦截器负责处理。

  1、当某个exception-mapping元素声明的异常被捕获到是,ExceptionMappingInterceptor拦截器就会向ValueStack中添加两个对象:

    exception:表示被捕获异常的Exception对象
    exceptionStack:包含着被捕获异常的栈

  2、什么地方注册这个拦截器?

struts-core.jar 下的struts-default.xml中<default-interceptor-ref name="defaultStack"/>

<interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>

 com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor怎么向ValueStack中写入异常信息?

package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.config.entities.ExceptionMappingConfig;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory; import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @author Matthew E. Porter (matthew dot porter at metissian dot com)
* @author Claus Ibsen
*/
public class ExceptionMappingInterceptor extends AbstractInterceptor { protected static final Logger LOG = LoggerFactory.getLogger(ExceptionMappingInterceptor.class); protected Logger categoryLogger;
protected boolean logEnabled = false;
protected String logCategory;
protected String logLevel; public boolean isLogEnabled() {
return logEnabled;
} public void setLogEnabled(boolean logEnabled) {
this.logEnabled = logEnabled;
} public String getLogCategory() {
return logCategory;
} public void setLogCategory(String logCatgory) {
this.logCategory = logCatgory;
} public String getLogLevel() {
return logLevel;
} public void setLogLevel(String logLevel) {
this.logLevel = logLevel;
} @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;
} /**
* Handles the logging of the exception.
*
* @param e the exception to log.
*/
protected void handleLogging(Exception e) {
if (logCategory != null) {
if (categoryLogger == null) {
// init category logger
categoryLogger = LoggerFactory.getLogger(logCategory);
}
doLog(categoryLogger, e);
} else {
doLog(LOG, e);
}
} /**
* Performs the actual logging.
*
* @param logger the provided logger to use.
* @param e the exception to log.
*/
protected void doLog(Logger logger, Exception e) {
if (logLevel == null) {
logger.debug(e.getMessage(), e);
return;
} if ("trace".equalsIgnoreCase(logLevel)) {
logger.trace(e.getMessage(), e);
} else if ("debug".equalsIgnoreCase(logLevel)) {
logger.debug(e.getMessage(), e);
} else if ("info".equalsIgnoreCase(logLevel)) {
logger.info(e.getMessage(), e);
} else if ("warn".equalsIgnoreCase(logLevel)) {
logger.warn(e.getMessage(), e);
} else if ("error".equalsIgnoreCase(logLevel)) {
logger.error(e.getMessage(), e);
} else if ("fatal".equalsIgnoreCase(logLevel)) {
logger.fatal(e.getMessage(), e);
} else {
throw new IllegalArgumentException("LogLevel [" + logLevel + "] is not supported");
}
} /**
* @deprecated since 2.3.15 please use #findMappingFromExceptions directly instead
*/
protected String findResultFromExceptions(List<ExceptionMappingConfig> exceptionMappings, Throwable t) {
ExceptionMappingConfig result = findMappingFromExceptions(exceptionMappings, t);
return result==null?null:result.getResult();
} /**
* Try to find appropriate {@link ExceptionMappingConfig} based on provided Throwable
*
* @param exceptionMappings list of defined exception mappings
* @param t caught exception
* @return appropriate mapping or null
*/
protected ExceptionMappingConfig findMappingFromExceptions(List<ExceptionMappingConfig> exceptionMappings, Throwable t) {
ExceptionMappingConfig config = null;
// Check for specific exception mappings.
if (exceptionMappings != null) {
int deepest = Integer.MAX_VALUE;
for (Object exceptionMapping : exceptionMappings) {
ExceptionMappingConfig exceptionMappingConfig = (ExceptionMappingConfig) exceptionMapping;
int depth = getDepth(exceptionMappingConfig.getExceptionClassName(), t);
if (depth >= 0 && depth < deepest) {
deepest = depth;
config = exceptionMappingConfig;
}
}
}
return config;
} /**
* Return the depth to the superclass matching. 0 means ex matches exactly. Returns -1 if there's no match.
* Otherwise, returns depth. Lowest depth wins.
*
* @param exceptionMapping the mapping classname
* @param t the cause
* @return the depth, if not found -1 is returned.
*/
public int getDepth(String exceptionMapping, Throwable t) {
return getDepth(exceptionMapping, t.getClass(), 0);
} private int getDepth(String exceptionMapping, Class exceptionClass, int depth) {
if (exceptionClass.getName().contains(exceptionMapping)) {
// Found it!
return depth;
}
// If we've gone as far as we can go and haven't found it...
if (exceptionClass.equals(Throwable.class)) {
return -1;
}
return getDepth(exceptionMapping, exceptionClass.getSuperclass(), depth + 1);
} /**
* 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);
} }

Struts(十二):异常处理:exception-mapping元素的更多相关文章

  1. python学习 (三十二) 异常处理

    1 异常: def exceptionHandling(): try: a = b = d = a / b print(d) except ZeroDivisionError as ex: print ...

  2. PHP学习笔记三十二【Exception】

    <?php // $fp=fopen("a.txt","r"); // echo "ok"; if(!file_exists(&quo ...

  3. WCF技术剖析之二十二: 深入剖析WCF底层异常处理框架实现原理[中篇]

    原文:WCF技术剖析之二十二: 深入剖析WCF底层异常处理框架实现原理[中篇] 在[上篇]中,我们分别站在消息交换和编程的角度介绍了SOAP Fault和FaultException异常.在服务执行过 ...

  4. 异常处理—Exception(二)

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

  5. Python开发【第二十二篇】:Web框架之Django【进阶】

    Python开发[第二十二篇]:Web框架之Django[进阶]   猛击这里:http://www.cnblogs.com/wupeiqi/articles/5246483.html 博客园 首页 ...

  6. (转)SpringMVC学习(十二)——SpringMVC中的拦截器

    http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...

  7. 二十二、OGNL的一些其他操作

    二十二.OGNL的一些其他操作 投影 ?判断满足条件 动作类代码: ^ $   public class Demo2Action extends ActionSupport {     public ...

  8. java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)

    java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessCo ...

  9. Spring+SpringMVC+MyBatis深入学习及搭建(十二)——SpringMVC入门程序(一)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6999743.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十一)——S ...

随机推荐

  1. EntityFramework Core 运行dotnet ef命令迁移背后本质是什么?(EF Core迁移原理)

    前言 终于踏出第一步探索EF Core原理和本质,过程虽然比较漫长且枯燥乏味还得反复论证,其中滋味自知,EF Core的强大想必不用我再过多废话,有时候我们是否思考过背后到底做了些什么,到底怎么实现的 ...

  2. python基础学习一 字符串的相关操作

    python的字符串 在python中,字符串是以unicode编码的,所以python的字符串支持多语言 对于单个字符的编码,python提供了ord()函数获取字符的整数表示,chr()函数是把编 ...

  3. 打印机驱动冲突和端口异常:win10更新部分补丁后,打印机本地连接(连接打印机的主机)可以打印,其他共享网络中的电脑可以连接到打印机,但不能打印——解决方案

    一.问题描述: 1.A(WIN10系统)表示连接打印机的电脑,P表示打印机(型号:惠普127M),B(WIN7系统)表示局域网中的电脑 2.A升级后部分补丁后,A可以使用打印机P打印文件,B显示可以连 ...

  4. spring boot 2.0.0由于版本不匹配导致的NoSuchMethodError问题解析

    spring boot升级到2.0.0以后,项目突然报出 NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBu ...

  5. Java多线程:CopyOnWrite容器

    一.什么是CopyOnWrite容器 CopyOnWrite容器即写时复制的容器.通俗的理解是当我们往一个容器添加元素的时候,不直接往当前容器添加,而是先将当前容器进行Copy,复制出一个新的容器,然 ...

  6. breeze源码阅读心得

            在阅读Spark ML源码的过程中,发现很多机器学习中的优化问题,都是直接调用breeze库解决的,因此拿来breeze源码想一探究竟.整体来看,breeze是一个用scala实现的基 ...

  7. 【R语言系列】作图入门示例一

    假设有如下数据,我们使用plot函数作图 月龄 体重 月龄 体重  1 4.4 9 7.3 3 5.3 3 6.0 5 7.2 9 10.4 2 5.2 12 10.2 11 8.5 3 6.1 R语 ...

  8. JDK1.8源码(六)——java.util.LinkedList 类

    上一篇博客我们介绍了List集合的一种典型实现 ArrayList,我们知道 ArrayList 是由数组构成的,本篇博客我们介绍 List 集合的另一种典型实现 LinkedList,这是一个有链表 ...

  9. Alpha冲刺博客集

    传送门 冲刺随笔 Alpha冲刺day1 (10.31):第一天博客地址 Alpha冲刺day2 (11.01):第二天博客地址 Alpha冲刺day3 (11.02):第三天博客地址 Alpha冲刺 ...

  10. 团队作业7——第二次项目冲刺(Beta版本)

    Deadline: 2017-12-10 23:00PM,以博客发表日期为准.   评分基准: 按时交 - 有分,检查的项目包括后文的三个方面 冲刺计划安排(单独1篇博客) 七天的敏捷冲刺(每两天发布 ...