首先,要跟大家道个歉,前一阵子为给客户个一个DEMO,忙得不可开交,所以很久没有更新Blog。提到这个DEMO我想顺便跟大家分享一下心得——如果大家希望快速开发,一个类似Struts 2这样的简单方便的WEB框架必不可少。我们在开发DEMO使用的还是Struts 1.2.8,而且没有不使用任何EL(表达式语言),导致页面出现无数类似
“<%= ((Integer) request.getAttribute("xx")).intValue()%6 %>”
的代码。Struts 1.x的Form Bean的麻烦使得有部分同事直接使用request.getParameter(String arg),继而引入另一种麻烦。诸如此类的问题,在DEMO这样时间紧迫的项目凸显了Struts 1.x对快速开发的无能为力。不过没办法,由于我们项目中的几个资深员工除了Struts 1.x外,对其它的WEB框架似乎不大感兴趣。
言归正传,Interceptor(以下译为拦截器)是Struts 2的一个强有力的工具,有许多功能(feature)都是构建于它之上,如国际化、转换器,校验等。
|
<script type="text/javascript"><!-- google_ad_client="pub-1832179689702023" ; google_ad_width="336;
google_ad_height" = 280; google_ad_format="336x280_as" ; google_ad_type="text_image" ;
//2007-01-10: 336_280 google_ad_channel="7111701428" ; google_color_border="FFFFFF" ; google_color_bg="FFFFFF" ; google_color_link="0000FF" ; google_color_text="000000" ; google_color_url="3D81EE" ;
//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script> |
<!--done-->
什么是拦截器
拦截器,在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所示。
图1 拦截器调用序列图
已有的拦截器
Struts 2已经为您提供丰富多样的,功能齐全的拦截器实现。大家可以到struts2-all-2.0.1.jar或struts2-core-2.0.1.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" />
配置和使用拦截器
在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页面后,查看服务器的后台输出。
2006 - 12 - 6 14 : 27 : 32 com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
信息: Executed action [ //Timer!execute ] took 2859 ms.
在您的环境中执行Timer!execute的耗时,可能上述的时间有些不同,这取决于您PC的性能。但是无论如何,2859 ms与500 ms还是相差太远了。这是什么原因呢?其实原因是第一次加载Timer时,需要进行一定的初始工作。当你重新请求Timer.action时,以上输出会变为:
2006 - 12 - 6 14 : 29 : 18 com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
信息: Executed action [ //Timer!execute ] took 500 ms.
OK,这正是我们期待的结果。上述例子演示了拦截器timer的用途——用于显示执行某个action方法的耗时,在我们做一个粗略的性能调试时,这相当有用。
分享到:
相关推荐
由于struts2标签的性能不好,项目组决定不使用,但是如果用struts2自带的拦截器防止重复提交又必须struts标签,所以只好自定拦器实现
由于struts2标签的性能不好,但是如果用struts2自带的拦截器防止重复提交又必须struts标签,所以只好自定拦器实现
4.1 拦截器在Struts2中的缺省应用 47 4.2 拦截器原理实现 50 4.3 在Struts2中配置自定义的拦截器 53 4.3.1 扩展拦截器接口的自定义拦截器配置 54 4.3.2 继承抽象拦截器的自定义拦截器配置 56 4.3.3 继承方法拦截器的...
7.开启struts2自带的开发模式常量 6 8.vo传参模式 7 9.ModerDriven传参模式(不建议采用) 7 10.为什么要使用struts2代替struts1.x 7 二、struts.xml配置及例程 7 1.配置文件的优先级 7 2.配置形式 8 3.package配置...
Struts2的核心组成部分是Action,拦截器和结果页。 Struts2提供了许多方法来创建Action类,并通过struts.xml中或通过注释进行配置。我们可以创建自己的拦截器实现常见任务。 Struts2中自带了很多的标签,并使用...
NULL 博文链接:https://handonghandong.iteye.com/blog/453603
2.6.1 struts 2内建拦截器介绍 95 2.6.2 定义和使用拦截器栈 97 2.6.3 使用自定义拦截器 98 2.7 小结 100 第3章 struts 2标签库详解与ajax技术应用 103 3.1 struts 2主题与模板 103 3.2 struts 2控制标签详解 ...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...
9.2 利用拦截器(Interceptor)生成审计日志 9.3 Hibernate的事件处理机制 9.4 批量处理数据 9.4.1 通过Session来进行批量操作 9.4.2 通过StatelessSession来进行批量操作 9.4.3 通过HQL来进行批量操作 ...