`
sxw7362693
  • 浏览: 59534 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类
最新评论

Struts2 学习笔记11--拦截器

阅读更多

拦截器
1、什么是拦截器
拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。
在 Webwork的中文文档的解释为——拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式。
谈到拦截器,还有一个词大家应该知道——拦截器链(Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。

2、实现原理
Struts 2的拦截器实现相对简单。当请求到达Struts 2的ServletDispatcher时,Struts 2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表(list),最后一个一个地调用列表中的拦截器,如图所示。


图1 拦截器调用序列图

 

 


3、已有的拦截器
Struts 2已经为您提供丰富多样的,功能齐全的拦截器实现。大家可以到struts2-all-2.0.1.jar或struts2-core.jar包的struts-default.xml查看关于默认的拦截器与拦截器链的配置
以下部分就是从struts-default.xml文件摘取的内容:

< 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 ="createSession" class ="org.apache.struts2.interceptor.CreateSessionInterceptor" />
< interceptor name ="debugging" class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
< interceptor name ="external-ref" class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" />
< 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 ="model-driven" class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />
< interceptor name ="scoped-model-driven" class ="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor" />
< interceptor name ="params" class ="com.opensymphony.xwork2.interceptor.ParametersInterceptor" />
< interceptor name ="prepare" class ="com.opensymphony.xwork2.interceptor.PrepareInterceptor" />
< interceptor name ="static-params" class ="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" />
< interceptor name ="scope" class ="org.apache.struts2.interceptor.ScopeInterceptor" />
< interceptor name ="servlet-config" class ="org.apache.struts2.interceptor.ServletConfigInterceptor" />
< interceptor name ="sessionAutowiring" class ="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" />
< interceptor name ="timer" class ="com.opensymphony.xwork2.interceptor.TimerInterceptor" />
< interceptor name ="token" class ="org.apache.struts2.interceptor.TokenInterceptor" />
< interceptor name ="token-session" class ="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
< interceptor name ="validation" class ="com.opensymphony.xwork2.validator.ValidationInterceptor" />
< 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" /> 
 


4、配置和使用拦截器
在struts-default.xml中已经配置了以上的拦截器。如果您想要使用上述拦截器,只需要在应用程序struts.xml文件中通过“<include file="struts-default.xml" />”将struts-default.xml文件包含进来,并继承其中的struts-default包(package),最后在定义Action时,使用“<interceptor-ref name="xx" />”引用拦截器或拦截器栈(interceptor stack)。一旦您继承了struts-default包(package),所有Action都会调用拦截器栈 ——defaultStack。当然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆盖defaultStack。

5、自定义拦截器

作为“框架(framework)”,可扩展性是不可或缺的,因为世上没有放之四海而皆准的东西。虽然,Struts 2为我们提供如此丰富的拦截器实现,但是这并不意味我们失去创建自定义拦截器的能力,恰恰相反,在Struts 2自定义拦截器是相当容易的一件事。
 
    大家在开始着手创建自定义拦截器前,切记以下原则:
拦截器必须是无状态的,不要使用在API提供的ActionInvocation之外的任何东西。
要求拦截器是无状态的原因是Struts 2不能保证为每一个请求或者action创建一个实例,所以如果拦截器带有状态,会引发并发问题。

所有的Struts 2的拦截器都直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor 。除此之外,大家可能更喜欢继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor


6、使用Token拦截器控制重复提交

jsp:
<form action="user" method="post">
            name:<input name="name">
            age:<input name="age">
            <input type="submit" value="add">
            <s:token></s:token>
</form>
生成的html:
<form action="user" method="post">
            name:<input name="name">
            age:<input name="age">
            <input type="submit" value="add">
            <input type="hidden" name="struts.token.name" value="struts.token" />
<input type="hidden" name="struts.token" value="85PAL2KOFS09RKVAP1FFBB985BJEGEUQ" />
</form>

 

其中token为令牌,服务端生成存储在session中并写入客户端页面,等用户提交form表单时会携带token,服务器比对token是否相同,相同则成功提交,并清除session中的token,如再重复提交服务端发现session中token已经没有了即失效,便返回Token失效页面。

 

Struts.xml:

     <action name="user" class="com.bjsxt.action.UserAction">
            <result>/addOK.jsp</result>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <interceptor-ref name="token"></interceptor-ref>
            <result name="invalid.token">/error.jsp</result>
        </action>

 

其中增加token的拦截器,会覆盖默认的defaultStack拦截器,所以需要再加上defaultStack在token前面,主要用来接收数据、声明异常、国际化等等拦截器功能。
如果出现重复提交,即点击多次add提交按钮,提交了多次,则后台认为token无效,返回error.jsp页面。


  • 大小: 18.4 KB
分享到:
评论

相关推荐

    struts2 学习重点笔记

    这是学习struts2时记得重点笔记,包括了一些原理,ognl语句的编写,以及如何设置拦截器等等一些基本知识,起到复习和巩固的作用

    struts2学习笔记

    struts2学习笔记,拦截器,action,所需jar包,从零开始

    Struts2 学习笔记

    01 Struts2-Action 5 一、 Struts作用: 5 二、 搭建Struts2的运行环境: 5 三、 Namespace 6 四、 标签 6 五、 Action 6 六、 路径问题的说明 8 ...09 自定义拦截器 38 10 类型转换 38 Struts2总结 39

    Struts2学习笔记

    struts2快速入门学习笔记,包含执行流程、验证、拦截器、国际化等的使用

    struts2学习笔记十(第10讲.Struts2的核心 拦截器)

    NULL 博文链接:https://zhaolianyang.iteye.com/blog/870600

    struts2.2学习笔记

    struts2.2学习笔记总结,action,拦截器,过滤器,国际化,struts.xml解析等的总结。

    struts项目学习笔记

    基于AOP(面向切面编程)思想的拦截器机制,更易扩展(不修改源代码的条件下,增强代码功能) 更强大、更易用输入校验功能 整合Ajax支持:json插件 Struts2的今生前世: 1.早期开发模型Servlet+JSP+JavaBean显得...

    struts2学习笔记十一(第11讲.Struts2的核心 拦截器续)

    NULL 博文链接:https://zhaolianyang.iteye.com/blog/872317

    Struts2.1学习笔记

    基于 Struts2.1.8 包括Struts2的基本应用、文件上传、拦截器、输入校验、国际化、OGNL表达式、Struts2标签等内容。

    struts拦截器 课堂笔记

    适合新手借鉴的struts学习笔记,更适合一入门的老手借鉴,个人学习心得

    struts学习笔记(2)

    当前struts2的filter过滤到一个.action结尾的请求的时候,会把这个请求交给struts2内部的拦截器(interceptor) 2)拦截器起到什么作用 可以帮我们丰富action的功能,比自动类型转换(页面传一个String类型的id,接收的...

    struts2入门笔记与源码

    学习struts2的笔记与源码:输入验证、国际化、拦截器等

    Struts 2.1.8_学习源码

    Struts 2.1.8 学习源码内容 Struts2_01FirstDemo : 跑通第一个Struts2的实例 Struts2_02CURD : 关于Struts2的增、删、...Struts2_05Interceptor : Struts2拦截器的使用 Struts2_06FileUpload : Struts2上传文件的使用

    struts2的学习笔记+测试源代码

    拦截器 博文链接:https://wuzhaohuixy-qq-com.iteye.com/blog/710102

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

    struts2框架学习项目,根据各个知识点分包分类实例

    struts2框架教学项目,针对每个知识点都有一个对应的实例,内嵌学习笔记。想学习的同学绝对适合,本人亲自教学实例。

    SpringMVC学习笔记整合搭建框架

    8、拦截器 2.Spring入门 2.1.Springmvc是什么 3.3.jdbc编程步骤: 1、加载数据库驱动 2、创建并获取数据库链接 3、创建jdbc statement对象 4、设置sql语句 5、设置sql语句中的参数(使用preparedStatement) 6、通过...

    跟我学javaweb全套ppt

    《跟我学Java Web》内容包括搭建Web开发环境、HTML相关技术基础知识、...Struts2技术详解(拦截器、输入校验、国际化、Struts2的各种标签、对Ajax的支持等)、Spring2.5(容器、装配Java Bean、JDBC和Hibernate模板...

Global site tag (gtag.js) - Google Analytics