Struts2的流程图

Struts2的处理流程

Struts2的拦截器中的分离关注

把过滤器要完成的事情委托给多个类完成,这种观点就是分离关注,过滤器负责调用这些类。

  • 处理Cookies的拦截器(处理web中的cookies)
  • 令牌拦截器(处理表单重复提交)
  • 参数拦截器
  • 文件上传拦截器
  • i18n拦截器(处理国际化)
    ……

  • 自定义拦截器

拦截器和过滤器的区别

相同点

  • 都是拦截作用

不同点

  • 过滤器是J2EE中的规范,任何javaWeb程序都可以使用
  • 拦截器是Struts2框架独有的,离开了Struts2框架,拦截器将不能使用,拦截器是依赖于Struts2框架的
  • 完成的功能不同

    • 过滤器负责拦截请求的路径,解析xml文件
    • 其他拦截功能交给拦截器处理
  • 调用顺序:过滤器——>拦截器

拦截器概述(struts-default.xml)


理解struts-default.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><!-- package:是struts2框架底层提供出来的* name:用于让其他包来继承的* abstract:设置为抽象包,下面不能定义action标签--><package name="struts-default" abstract="true"><!-- result-types:声明结果类型* name:结果类型的名称* class:结果类型对应类的完整路径* default:设置其为默认,true是默认--><result-types><!-- 转发到action --><result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/><!-- 转发到jsp --><result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/><result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/><result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/><!-- 重定向到jsp --><result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/><!-- 重定向到action -->                                           <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/><!-- 用于下载 --><result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/><result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/><result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/><result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /></result-types><!-- interceptors* interceptor:声明拦截器* name:拦截器的名称* class:对应拦截器类的完整路径--><interceptors><interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/><interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/><interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/><interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/><interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/><interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" /><interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" /><interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" /><interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/><interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/><interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/><interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/><interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/><interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/><interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/><interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/><interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/><interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/><interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/><interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/><interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/><interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/><interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/><interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/><interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/><interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/><interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" /><interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" /><interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" /><interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" /><interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" /><interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" /><!-- Basic stack --><interceptor-stack name="basicStack"><interceptor-ref name="exception"/><interceptor-ref name="servletConfig"/><interceptor-ref name="prepare"/><interceptor-ref name="checkbox"/><interceptor-ref name="multiselect"/><interceptor-ref name="actionMappingParams"/><interceptor-ref name="params"><param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param></interceptor-ref><interceptor-ref name="conversionError"/></interceptor-stack><!-- interceptor-stack:拦截器栈* struts2框架通过使用拦截器栈,进而使用上面声明好的拦截器* 在拦截器栈里面,存放了一些上面声明好的拦截器* 拦截器栈相当于一个list集合,执行的时候是按照存放的先后顺序来执行--><interceptor-stack name="defaultStack"><interceptor-ref name="exception"/><interceptor-ref name="alias"/><interceptor-ref name="servletConfig"/><interceptor-ref name="i18n"/><interceptor-ref name="prepare"/><interceptor-ref name="chain"/><interceptor-ref name="scopedModelDriven"/><interceptor-ref name="modelDriven"/><interceptor-ref name="fileUpload"/><interceptor-ref name="checkbox"/><interceptor-ref name="multiselect"/><interceptor-ref name="staticParams"/><interceptor-ref name="actionMappingParams"/><interceptor-ref name="params"><param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param></interceptor-ref><interceptor-ref name="conversionError"/><interceptor-ref name="validation"><param name="excludeMethods">input,back,cancel,browse</param></interceptor-ref><interceptor-ref name="workflow"><param name="excludeMethods">input,back,cancel,browse</param></interceptor-ref><interceptor-ref name="debugging"/></interceptor-stack></interceptors><!-- 配置在struts2框架运行时,默认要执行的是哪个拦截器栈,defaultStack --><default-interceptor-ref name="defaultStack"/><!-- 配置在struts2框架运行时,如果没有为action指定class的话,默认要执行的class的类名 --><default-class-ref class="com.opensymphony.xwork2.ActionSupport" /></package></struts>

拦截器概述(演示过滤器和拦截器的执行顺序)

使用如下三个拦截器演示struts的执行流程(断点演示)
默认的是defaultStack

<interceptor name="chain"
class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/><interceptor name="params"
class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/><interceptor name="prepare"
class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>

在栈中的顺序如下:

Struts2拦截器入门相关推荐

  1. Struts2拦截器-day04下

    目录 第一节 Strust2的拦截器 1.1 拦截器的重要性 1.2 拦截器的执行时机 1.3 拦截器的部分类结构 1.4 简单拦截器入门 单个拦截器 多个拦截器 1.5 拦截器的应用场景(登录) 案 ...

  2. Struts2拦截器的使用

    如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈defaultStack,如下(这里我是引用了str ...

  3. Struts2拦截器属性excludeMethods、includeMethods配置无效之解决方法

    参考:http://blog.csdn.net/coolcoffee168/article/details/7963251 在配置struts2 拦截器属性excludeMethods.include ...

  4. Struts2 拦截器 及如何获得 servlet 请求对象 以及Struts 基本配置 Session 超时设置...

    在拦截器中可以三种实现 一:继承 AbstractInterceptor 类 二:继承 MethodFilterInterceptor类 三:实现 Interceptor 接口 在实现Intercep ...

  5. 基于struts2拦截器实现用户操作日志记录

    2019独角兽企业重金招聘Python工程师标准>>> 这里基于struts2的拦截器来实现. 使用struts2拦截器拦截所有或者指定的请求,对用户操作过程中的:操作用户,操作时间 ...

  6. Struts2拦截器实现异常处理机制

    http://bbs.itcast.cn/thread-10364-1-1.html Struts2拦截器实现异常处理机制   在j2ee项目中,系统内部难免会出现一些异常,如果把异常放任不管直接打印 ...

  7. 从struts2拦截器到自定义拦截器

    http://www.cnblogs.com/withyou/p/3170440.html 拦截器可谓struts2的核心了,最基本的bean的注入就是通过默认的拦截器实现的,一般在struts2.x ...

  8. Struts2拦截器的使用 (详解)

    Struts2拦截器的使用 (详解) 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈default ...

  9. struts2 拦截器_Struts2令牌拦截器示例

    struts2 拦截器 Struts 2 token interceptor can be used to handle multiple form submission problem. While ...

最新文章

  1. 从JoinBatchGroup 代码细节 来看Rocksdb的相比于leveldb的写入优势
  2. html 一条线两种颜色,HTML5/CSS3超酷进度条 不同进度多种颜色
  3. 【Android 逆向】代码调试器开发 ( 代码调试器功能简介 | 设置断点 | 读写内存 | 读写寄存器 | 恢复运行 | Attach 进程 )
  4. Django(part12)--模板的标签
  5. “写画”与“惊春”:王艾画展和当代诗人朗诵会
  6. JVM学习笔记(二):垃圾收集
  7. python35个关键字_Python关键字35个
  8. 只有得到祝福才是好婚姻
  9. iphone各机型参数对比_「科技美学」又是一年“双十一”,畅销机型有哪些?
  10. java CPU 占用过高
  11. 15.高性能MySQL --- 备份与恢复
  12. Python爬虫:数据提取
  13. SI 9000 阻抗计算笔记
  14. 做最好的自己——摘自李镇西的《做最好的老师》
  15. 图像处理--传统的手工特征之HOG
  16. python彩票分析_128期老铁大乐透预测奖号:大中小码分析
  17. [转]一个手机游戏的服务器架构
  18. 【HTML5系列教程】
  19. 2023亚马逊云科技中国峰会引领无服务器架构新潮流:Serverlesspresso Workshop
  20. php用什么系统好_有哪些免费开源好用的PHP语言CMS系统?

热门文章

  1. 计算机网络原理 7层结构概述
  2. SEO关键词排名-百度点击软件(刷关键词排名、百度指数、百度相关、竞价点击-全能软件)
  3. √ C# - 19.怎么使用反射(P528)
  4. 【无标题】使用Ping命令制作的批量Ping工具,可对指定Ip进行ping测试,并将结果记录到txt文件
  5. UltralSO制作启动盘显示报错:磁盘/映像容量太小
  6. macos终端美化_关于macOS终端美化的最轻松的指南Z Shell中的速成课程
  7. RAID的使用和区别
  8. PTA 7-144 最矮的巨人
  9. 从哲学角度理解面向对象的思想
  10. EmguCv模板匹配