Struts2默认拦截器(ExceptionMappingInterceptor)的使用及源码阅读

永磁同步电机定子槽型设计实战:从梨形槽到矩形槽的NVH优化之路 本文深入探讨了永磁同步电机定子槽型设计在NVH优化中的关键作用,对比了梨形槽与矩形槽的性能差异。通过Ansys Maxwell仿真和实际案例,揭示了矩形槽在提升槽满率、降低电磁噪声方面的优势,并总结了五大设计黄金法则,为电机设计工程师提供了实用指导。 阅读详情

定义

捕获异常并定位到指定视图。
ps:从这给类的原型,可以帮助我们思考异常如何以日志形式记录,可以再此类的基础上进行扩展。这个待日后有慢慢琢磨。

使用

1.局部exception配置使用

Action源码:
[java]  view plain copy print ?
  1. public class SourceCoreAction {  
  2.    
  3.     public String error() throws ClassNotFoundException {  
  4.         System.out.println("do the error!");      
  5.         Class.forName("xxx");         
  6.         return "success";  
  7.     }  
  8.       
  9.  }  
struts-xxx.xml配置:
[html]  view plain copy print ?
  1. <span style="white-space:pre">      </span><action name="error" class="com.company.strutstudy.web.action.core.SourceCoreAction"  
  2.             method="error">  
  3.             <interceptor-ref name="exception"></interceptor-ref><!--只有一个默认拦截器起作用,好debug-->  
  4.             <result name="input">/core/exception.jsp</result>  
  5.             <result name="success">/core/ok.jsp</result>  
  6.             <exception-mapping result="input" exception="java.lang.Exception"></exception-mapping>  
  7.         </action>  

2.全局exception配置使用

Action源码不变,参考局部exception

struts-xxx.xml配置:
[html]  view plain copy print ?
  1. <struts>  
  2.     <package name="exception" extends="struts-default" abstract="true">  
  3.         <global-results>  
  4.             <result name="input">/core/exception.jsp</result>  
  5.         </global-results>  
  6.         <global-exception-mappings>  
  7.             <exception-mapping result="input" exception="java.lang.Exception"></exception-mapping>  
  8.         </global-exception-mappings>  
  9.     </package>  
  10.     <package name="core" extends="exception">  
  11.         <action name="error"class="com.company.strutstudy.web.action.core.SourceCoreAction" method="error">  
  12.             <interceptor-ref name="exception"></interceptor-ref>  
  13.             <result name="success">/core/ok.jsp</result>  
  14.         </action>  
  15.     </package>  
  16.  </struts>  

源码阅读

[java]  view plain copy print ?
  1. public class ExceptionMappingInterceptor extends AbstractInterceptor {  
  2.        
  3.      protected static final Log log = LogFactory.getLog(ExceptionMappingInterceptor.class);  
  4.      protected Log categoryLogger;  
  5.      protected boolean logEnabled = false;  
  6.      protected String logCategory;  
  7.      protected String logLevel;      
  8.    
  9.      ....getset省略  
  10.      public String intercept(ActionInvocation invocation) throws Exception {  
  11.          String result;  
  12.          /* 
  13.           * 处理action或拦截器执行的异常 
  14.           */  
  15.          try {  
  16.              result = invocation.invoke();  
  17.          } catch (Exception e) {  
  18.             //默认为false  
  19.              if (logEnabled) {  
  20.                  handleLogging(e);  
  21.              }  
  22.              //获取全局或局部与该action相关的exception映射信息  
  23.              List exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings();  
  24.              //获取异常发生以后的逻辑视图(resultcode)  
  25.              String mappedResult = this.findResultFromExceptions(exceptionMappings, e);  
  26.              if (mappedResult != null) {  
  27.                  result = mappedResult;  
  28.                  //将异常信息压入栈顶,异常信息可在页面通过ognl进行获取。  
  29.                  publishException(invocation, new ExceptionHolder(e));  
  30.              } else {  
  31.                  throw e;  
  32.              }  
  33.          }  
  34.    
  35.          return result;  
  36.      }  
  37.    
  38.      /** 
  39.       * Handles the logging of the exception. 
  40.       *  
  41.       * @param e  the exception to log. 
  42.       */  
  43.      protected void handleLogging(Exception e) {  
  44.         if (logCategory != null) {  
  45.             if (categoryLogger == null) {  
  46.                 // init category logger  
  47.                 categoryLogger = LogFactory.getLog(logCategory);  
  48.             }  
  49.             doLog(categoryLogger, e);  
  50.         } else {  
  51.             doLog(log, e);  
  52.         }  
  53.      }  
  54.        
  55.      /** 
  56.       * Performs the actual logging. 
  57.       *  
  58.       * @param logger  the provided logger to use. 
  59.       * @param e  the exception to log. 
  60.       */  
  61.      protected void doLog(Log logger, Exception e) {  
  62.         if (logLevel == null) {  
  63.             logger.debug(e.getMessage(), e);  
  64.             return;  
  65.         }     
  66.         if ("trace".equalsIgnoreCase(logLevel)) {  
  67.             logger.trace(e.getMessage(), e);  
  68.         } else if ("debug".equalsIgnoreCase(logLevel)) {  
  69.             logger.debug(e.getMessage(), e);  
  70.         } else if ("info".equalsIgnoreCase(logLevel)) {  
  71.             logger.info(e.getMessage(), e);  
  72.         } else if ("warn".equalsIgnoreCase(logLevel)) {  
  73.             logger.warn(e.getMessage(), e);  
  74.         } else if ("error".equalsIgnoreCase(logLevel)) {  
  75.             logger.error(e.getMessage(), e);  
  76.         } else if ("fatal".equalsIgnoreCase(logLevel)) {  
  77.             logger.fatal(e.getMessage(), e);  
  78.         } else {  
  79.             throw new IllegalArgumentException("LogLevel [" + logLevel + "] is not supported");  
  80.         }  
  81.      }  
  82.    
  83.      private String findResultFromExceptions(List exceptionMappings, Throwable t) {  
  84.          String result = null;  
  85.    
  86.          // Check for specific exception mappings.  
  87.          if (exceptionMappings != null) {  
  88.              int deepest = Integer.MAX_VALUE;  
  89.              for (Iterator iter = exceptionMappings.iterator(); iter.hasNext();) {  
  90.                  ExceptionMappingConfig exceptionMappingConfig = (ExceptionMappingConfig) iter.next();  
  91.                  int depth = getDepth(exceptionMappingConfig.getExceptionClassName(), t);  
  92.                  if (depth >= 0 && depth < deepest) {  
  93.                      deepest = depth;  
  94.                      result = exceptionMappingConfig.getResult();  
  95.                  }  
  96.              }  
  97.          }  
  98.    
  99.          return result;  
  100.      }  
  101.    
  102.      /** 
  103.       * Return the depth to the superclass matching. 0 means ex matches exactly. Returns -1 if there's no match. 
  104.       * Otherwise, returns depth. Lowest depth wins. 
  105.       * 
  106.       * @param exceptionMapping  the mapping classname 
  107.       * @param t  the cause 
  108.       * @return the depth, if not found -1 is returned. 
  109.       */  
  110.      public int getDepth(String exceptionMapping, Throwable t) {  
  111.          return getDepth(exceptionMapping, t.getClass(), 0);  
  112.      }  
  113.    
  114.      private int getDepth(String exceptionMapping, Class exceptionClass, int depth) {  
  115.          if (exceptionClass.getName().indexOf(exceptionMapping) != -1) {  
  116.              // Found it!  
  117.              return depth;  
  118.          }  
  119.          // If we've gone as far as we can go and haven't found it...  
  120.          if (exceptionClass.equals(Throwable.class)) {  
  121.              return -1;  
  122.          }  
  123.          return getDepth(exceptionMapping, exceptionClass.getSuperclass(), depth + 1);  
  124.      }  
  125.    
  126.      /** 
  127.       * Default implementation to handle ExceptionHolder publishing. Pushes given ExceptionHolder on the stack. 
  128.       * Subclasses may override this to customize publishing. 
  129.       * 
  130.       * @param invocation The invocation to publish Exception for. 
  131.       * @param exceptionHolder The exceptionHolder wrapping the Exception to publish. 
  132.       */  
  133.      protected void publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder) {  
  134.          invocation.getStack().push(exceptionHolder);  
  135.      }  
  136.  }  
【python股票价格预测】基于CNN-LSTM的股票价格预测(附python代码) 阅读详情

相关推荐

DC综合中set_dft_signal的实战配置与优化策略

本文深入解析了DC综合中set_dft_signal命令的实战配置与优化策略。文章从命令的核心作用出发,详细拆解了-view、-type、-usage等关键参数,并通过扫描使能与时钟门控的实战案例,展示了如何正确配置以实现测试模式下的功耗优化。同时,文章总结了高级优化策略、与低功耗流程的协同方法以及常见错误排查清单,旨在帮助数字芯片设计工程师构建高可测性、低功耗的稳健DFT基础。

weixin_28347369的博客 403

struts2 18拦截器详解(二) --- ExceptionInterceptor

exception拦截器 exception拦截器对应的类为com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor 该拦截器是struts2专门用于处理异常的,如果我们进行了异常配置,当struts2的执行出现异常后可以跳转到指定的Result,这也是struts2的异常处理机制。   下面看一下该拦截器的源码:

云原生之家 5866

【GD32F407】i2c主机发送,从机中断接收

前言 现在手里负责的一个项目需要用i2c和主芯片进行通信主动发送,中断接收,这就需要支持i2c主机模式和从机模式,先查看原理图 这里我们用到了两条i2c总线进行数据的交替传输,避免在使用一条总线是发送数据和接收数据冲突 初始化i2c控制器 s32_k i2cDevInit(u8 byBusId, TI2cBus *pTI2cBus) { //PCLK = 36000000 BOOL retFlg = FALSE; int I2xSCL = pTI2cBus->byFastMode ?

Enosji的博客 6246

struts2异常:ERROR ExceptionMappingInterceptor No result defined for action

出现这种异常是因为前端页面传过来的参数值和struts2定义的参数值类型内容不符,就是action里面定义的全局变量如果定义的是integer类型的,但是前端input框传过来的是double的话,就会出现这种异常,所以出现这种错误可以检查下前端传值内容是否符合后台定义的参数类型。 ...

fhx308050的博客 1046

naca翼型_STARCCM+计算二维翼型气动性能

STAR-CCM+计算二维翼型气动性能一算例背景机翼理论主要研究翼型在流体中运动时的力学特性。在工程领域中,机翼以升力面、控制面、叶片或桨叶等形式出现。舰船上的舵、水翼、减摇鳍等都是机翼,螺旋桨、汽轮机叶片和压缩机叶片也都是利用机翼原理工作的,而在研究船舶操纵性时,甚至还可把船体的水下部分看作一个巨大的机翼。随着航空科学的发展,世界各主要航空发达的国家建立了各种翼型系列。美国有NACA系...

weixin_39863741的博客 1万+

从零到一:RagFlow源码架构深度解析与二次开发实战指南

本文深入解析RagFlow源码架构,提供从环境搭建到二次开发的全流程实战指南。作为企业级AI搜索框架,RagFlow通过模块化设计和多模态处理能力,支持自定义智能体开发与文搜文档功能,帮助开发者高效构建智能搜索系统。

w1x2y3的博客 267

Struts2拦截器之ExceptionMappingInterceptor(异常映射拦截器)

一、异常拦截器是什么? 异常拦截器的作用是提供一个机会,可以设置在action执行过程中发生异常的时候映射到一个结果字符串而不是直接中断。 将异常整合到业务逻辑中,比如在分层系统的调用中可以从底层抛出一个异常,高层捕捉到这个异常就知道发生了什么事情啦。 二、如何使用? 1.两种异常映射类型: 1.1.global global的异常映射对整个package下的action都有...

weixin_30800987的博客 460

【TM1650】STM8S TM1650芯片控制程序

以下为.h文件: #ifndef __TM1650_I2C_H #define __TM1650_I2C_H #include "stm8s.h" #include "stm8s_gpio.h" #include "tim1.h" #include "usart2.h" #include &lt;iostm8s103f3.h&gt; #include &lt;intrinsics....

cxw312864660的博客 1万+

java 设计模式 拦截器

拦截器(interceptor)是Struts2最强大的特性之一,也可以说是struts2的核心,拦截器可以让你在Action和result被执行之前或之后进行一些处理。同时,拦截器也可以让你将通用的代码模块化并作为可重用的类。Struts2中的很多特性都是由拦截器来完成的。拦截是AOP的一种实现策略。在Webwork的中文文档的解释为:拦截器是动态拦截Action调用的对象。它提供了一种机制可以使

ooiuy450的专栏 4728

abapGit 安装与初始化完整指南:从 on-premise 到 SAP BTP ABAP environment 的一条龙实践

本文为ABAP开发者提供abapGit从安装到团队协作的完整指南。内容涵盖社区版ZABAPGIT的on-premise安装、Eclipse ADT插件配置、BTP环境使用及常见网络问题处理。重点包括:获取最新构建的安装步骤、EN语言环境的重要性、ADT插件安装方法、以及如何实现on-premise与BTP间的代码迁移。文中特别强调企业环境下代理设置、证书配置等注意事项,并提供实际操作案例,如将RAP业务对象迁移到BTP环境。文末附有可复用的实施清单,帮助开发者快速落地abapGit。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 417

【RT-DETR有效改进】计算训练好权重文件对应的FPS、推理每张图片的平均时间(科研必备)

本文给大家带来的改进机制是利用我们训练好的权重文件计算FPS,同时打印每张图片所利用的平均时间,模型大小(以MB为单位),同时支持batch_size功能的选择,对于轻量化模型的读者来说,本文的内容对你一定有帮助,可以清晰帮你展示出模型速度性能的提升以及轻量化的效果(模型大小),对于以提高精度为目的的读者本文也能够帮助大家展示出现阶段的模型速度指标。所以本文的内容是十分有用的机制,对于大家发表论文来说,下面的图片为运行后的效果可以看到,该有的指标均已打印(本文内容为我独家创新,全网无第二份)下面是效果图和文

Snu77的博客 3629

ExceptionMappingInterceptor 总结

This interceptor forms the core functionality of the exception handling feature. Exception handling allows you to map an exception to a result code, just as if the action returned a result code inste...

yaozuodaoforfly的博客 652

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

MyBatis的数据库异常处理与错误提示

1.背景介绍 MyBatis的数据库异常处理与错误提示 作者:禅与计算机程序设计艺术 1. 背景介绍 1.1 MyBatis简介 MyBatis是一款优秀的半自动ORM框架,它提供了对JDBC的封装,使开发人员能够使用类似SQL的语言编写数据访问代码,同时又能够利用ORM的特性进行高效的数据映射和操作。 1.2 数据库异常处理的重要性 在使用MyBatis对数据库进行CRUD操作时,...

AI天才研究院 2700

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

java Internal error in the mapping processor java.lang.NullPointerException解决方案

【代码】java Internal error in the mapping processor java.lang.NullPointerException解决方案。

哟,你又在偷偷写bug! 1442

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

java Internal error in the mapping processor java.lang.NullPointerException解决方法

java: Internal error in the mapping processor: java.lang.NullPointerException at org.mapstruct.ap.internal.processor.DefaultVersionInformation.createManifestUrl(DefaultVersionInformation.java:180) at org.mapstruct.ap.internal.processor.DefaultVersionIn

kongkong的专栏 4054

Struts2拦截器--异常拦截器

Struts2的异常拦截器是默认的拦截器,所以只需要声明引入就可以使用  接下来就是定义异常类型了,我这里只定义了全局异常:/succ.jsp/login.jsp/wait.jsp/error.jsp  当action里面发生异常的话,将被异常拦截器拦截,并返回error,接着跳转到error.jsp 

beckham 1671

Interceptor+Token拦截器登录校验 + Exception全局异常处理器

使用拦截器完成用户鉴权,解决重复性的登录验证和token解析;统一处理项目中的异常,减少项目中的try...catch的代码冗余

weixin_44894196的博客 1654

springmvc 异常Interceptor

无论做什么项目,进行异常处理都是非常有必要的,而且你不能把一些只有程序员才能看懂的错误代码抛给用户去看,所以这时候进行统一的异常处理,展现一个比较友好的错误页面就显得很有必要了。   springMVC提供的异常处理主要有两种方式,一种是直接实现自己的HandlerExceptionResolver,当然这也包括使用spring已经为我们提供好的SimpleMappingExceptionRe...

weixin_34054866的博客 164

Struts2的异常处理机制、自定义拦截器

异常处理: 首先,action的可执行方法,一般是声明抛出异常的。 1. 我们可以在代码中自己定义抛出的异常(这里定义RuntimeException),即抛出异常 2. 在配置文件struts.xml中,配置注册异常映射 当出现java.lang.RuntimeExceptio...

chousheng1613的博客 251

struts2配置全局异常拦截

[b]把这段配置放到package中就行了....[/b] [code="xml"] /error.jsp [/code] 其实也可以配置一个拦截器,拦截invocation.invoke()是否有异常,有异常则设置错误信息返回到指定页面。请看下面代码: 1.struts.xml的配置:[code="xml"] ...

爱,促进自我完善! 432

5.Struts 2拦截器(Interceptor)

任何优秀的MVC框架都会提供一些通用的操作,如请求数据的封装、类型转换、数据校验、解析上传的文件、防止表单的多次提交等。 Struts 2将它的核心功能放到拦截器中实现而不是集中在核心控制器中实现,把大部分控制器需要完成的工作按功能分开定义,每个拦截器完成一个功能,而完成这些功能的拦截器可以自由选择、灵活组合,需要哪些只需要在struts.xml指定即可,从而增加了框架的灵活性,有利于系统解耦。 拦截器的方法在Action执行之前或者执行之后自动的执行,从而将通用的操作动态地插入到了可插拔式。

相互学习 共同进步 1039

第九章 拦截器(Interceptors)

第九章 拦截器 拦截器的工作原理如上图,每一个Action请求都包装在一系列的拦截器的内部。拦截器可以在Action执行直线做相似的操作也可以在Action执行直后做回收操作。 每一个Action既可以将操作转交给下面的拦截器,Action也可以直接退出操作返回客户既定的画面。 如何自定义一个拦截器?自定义一个拦截器需要三步:1 自定义一个实现Interce

Struts 2 的专栏 2万+

struts2之拦截器详解

文章目录1 拦截器1.1 拦截器是什么1.2 拦截器的优点是什么1.3 预定义拦截器1.3.1 预定义拦截器1.3.1.1 params拦截器1.3.1.2 timer拦截器1.3.1.3 logger拦截器1.3.2 拦截器配置说明1.3.3 拦截器的调用顺序1.3.4 全局拦截器的配置1.3.5 使用execAndWait拦截器1.4 自定义拦截器1.4.1 自定义基础拦截器1.4.2 自定义拦截器示例1.4.3 自定义登录检查拦截器1.4.4 自定义logger拦截器 1 拦截器 1.1 拦截器是什么

上善若泪 2827
下一篇: 通过debug过程分析Struts2什么时候将Action对象放入了值栈ValueStack中
KnightKitt
博客等级 码龄12年 1粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值