博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts(十二):异常处理:exception-mapping元素
阅读量:6590 次
发布时间:2019-06-24

本文共 8876 字,大约阅读时间需要 29 分钟。

  • 配置当前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:

 
/error.jsp
/details.jsp
 

添加在WebContent下添加error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  Insert title here  error

重启服务,重新访问页面,并提交参数,发现页面自动跳转到了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"%>
Insert title here error
${exception}
${exception.message}
${exceptionStack}

显示信息:

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

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

 

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

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

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

/error.jsp
/details.jsp

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

  • 声明式异常处理机制由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
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
(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
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
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); }}

 

转载地址:http://tukio.baihongyu.com/

你可能感兴趣的文章
南开大学提出新物体分割评价指标,相比经典指标错误率降低 69.23%
查看>>
初创公司MindMaze研发情绪反应VR,让VR关怀你的喜怒哀乐
查看>>
绕开“陷阱“,阿里专家带你深入理解C++对象模型的特殊之处
查看>>
ElasticSearch
查看>>
9-51单片机ESP8266学习-AT指令(测试TCP服务器--51单片机程序配置8266,C#TCP客户端发信息给单片机控制小灯的亮灭)...
查看>>
香港设计师带来仿生机器人,其身体 70% 构造均由3D打印完成
查看>>
bootstrap16-上下文表格布局
查看>>
不规则物体形状匹配综述
查看>>
自动化设计-框架介绍 TestCase
查看>>
CJ看showgirl已经out!VR体验才是王道
查看>>
postgresql 数组类型
查看>>
Vue+Webpack常见问题(持续更新)
查看>>
栈与递归的实现
查看>>
Manually Summarizing EIGRP Routes
查看>>
spring boot 1.5.4 整合webService(十五)
查看>>
modsecurity(尚不完善)
查看>>
获取.propertys文件获取文件内容
查看>>
Redis3.0.5配置文件详解
查看>>
Keepalived+Nginx实现高可用
查看>>
Know about Oracle RAC Heartbeat
查看>>