`
foxpro
  • 浏览: 26904 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Struts 2的拦截器(Interceptor)与实例说明

阅读更多
Interceptor(以下译为拦截器)是Struts 2的一个强有力的工具,有许多功能(feature)都是构建于它之上,如国际化、转换器,校验等。

什么是拦截器
拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。

在Webwork的中文文档的解释为——拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式。

谈到拦截器,还有一个词大家应该知道——拦截器链(Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。

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




已有的拦截器
Struts 2已经为您提供丰富多样的,功能齐全的拦截器实现。大家可以到struts2-all-2.0.1.jar或struts2-core-2.0.1.jar包的struts-default.xml查看关于默认的拦截器与拦截器链的配置。

  在本文使用是Struts 2的最新发布版本2.0.1。需要下载的朋友请点击以下链接:
http://apache.justdn.org/struts/binaries/struts-2.0.1-all.zip

以下部分就是从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" />
配置和使用拦截器
在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。

下面是关于拦截器timer使用的例子。首先,新建Action类tuotrial/TimerInterceptorAction.java,内容如下:

package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class TimerInterceptorAction extends ActionSupport  {
    @Override
     public String execute()  {
         try  {
             // 模拟耗时的操作
            Thread.sleep( 500 );
        } catch (Exception e)  {
            e.printStackTrace();
        }
         return SUCCESS;
    }
}
配置Action,名为Timer,配置文件如下:

<! DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
    < include file ="struts-default.xml" />   
    < package name ="InterceptorDemo" extends ="struts-default" >
        < action name ="Timer" class ="tutorial.TimerInterceptorAction" >
            < interceptor-ref name ="timer" />
            < result > /Timer.jsp </ result >
        </ action >
    </ package >
</ struts >
至于Timer.jsp可以随意写些什么到里面。发布运行应用程序,在浏览器的地址栏键入http://localhost:8080/Struts2_Interceptor/Timer.action,在出现Timer.jsp页面后,查看服务器的后台输出。

分享到:
评论

相关推荐

    struts2 拦截器实例

    struts2,Interceptor struts2拦截器实例,两套实例,一套是针对单个的action配置的,另一套实例是针对全局的action配置的拦截器interceptor

    Struts拦截器及token拦截器防止重复提交例子源码

    Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的 拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器 三、定义Struts2拦截器。 ...

    使用struts2拦截器对登陆权限验证

    综合使用servlet filter与struts2 interceptor对权限进行验证。

    Struts2拦截器Interceptor的原理与配置实例详解

    拦截器是一种AOP(面向切面编程)思想的编程方式.它提供一种机制是开发者能够把相对独立的代码抽离出来,...下面这篇文章主要给大家介绍了关于Struts2拦截器Interceptor的原理与配置的相关资料,需要的朋友可以参考下。

    Java中的Struts2框架拦截器之实例代码

    本文实例为大家分享了Struts2框架拦截器实例的示例代码,供大家参考,具体内容如下 在看拦截器的小例子的前我们先来看看sturts2的...拦截器interceptor体现了一种编程理念,叫做AOP(面向切面编程) 实例1:使用token

    Struts 2.1.8_学习源码

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

    struts2入门实例1

    struts2 最新的入门实例 我自己总结的 呵呵,欢迎提出宝贵的意见 1.Struts2_01_login 对应登录。。 login.jsp---------------------------------------html标签的登陆页面 login_struts2.jsp--------------...

    struts2入门实例2 经典入门必备

    1.Struts2_01_login 对应登录。。 ... login_struts2.jsp-------------------------------采用struts标签的登陆页面 ... login_struts_validate_noblank.jsp-----------... 拦截器 17.Struts2_09up&down 上传和下载

    Struts2入门教程(全新完整版)

    十二、总结 本教程对struts2的基本知识进行了一些说明,关于struts2的更多详细内容应参看struts2的官方文档及提供的app实例。 下面对struts2的基本执行流程作一简要说明,此流程说明可以结合官方提供的struts2结构图...

    struts2入门实例4 经典入门必备

    1.Struts2_01_login 对应登录。。 ... login_struts2.jsp-------------------------------采用struts标签的登陆页面 ... login_struts_validate_noblank.jsp-----------... 拦截器 17.Struts2_09up&down 上传和下载

    struts2入门实例3 经典入门必备

    1.Struts2_01_login 对应登录。。 ... login_struts2.jsp-------------------------------采用struts标签的登陆页面 ... login_struts_validate_noblank.jsp-----------... 拦截器 17.Struts2_09up&down 上传和下载

    struts2流程与流程图

    但在调用之前,ActionInvocation会根据配置加载Action相关的所有Interceptor(拦截器)。  一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果result。 Struts 2的核心控制器是...

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    如数据类型转换、文件上传和下载、Struts2应用的安全性、调试与性能分析、FreeMarker、Velocily、Ajax,等等。跟随作者一道深入Struts2。聆听大量来之不易的经验之谈。你对Struts2开发框架的理解和应用水平都将更上...

    StrutsInterceptor

    本实例是模拟Struts2的MVC模式,是Struts2的拦截器,简单易懂

    J2EE应用开发详解

    118 8.3.5 Struts2配置文件 119 8.4 Action的配置方式 121 8.4.1 动态方法调用 121 8.4.2 设置action元素的method属性 122 8.4.3 使用通配符配置action 122 8.4.4 默认action 123 8.5 拦截器Interceptor 123 8.5.1 ...

    Java学习笔记-个人整理的

    {12.5}操作符与实例}{154}{section.12.5} {12.5.1}where}{154}{subsection.12.5.1} {12.6}函数}{156}{section.12.6} {12.7}组函数}{158}{section.12.7} {12.7.1}group by}{159}{subsection.12.7.1} {12.7.2}...

Global site tag (gtag.js) - Google Analytics