专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »博文摘选 » interceptor:Struts 2的基石——拦截器(Interceptor) »正文

interceptor:Struts 2的基石——拦截器(Interceptor)

来源: 发布时间:星期二, 2009年12月8日 浏览:0次 评论:0
转自: http://www.blogjava.net/max/archive/2006/12/06/85925.html     max _disibledevent=> 言归正传Interceptor(以下译为拦截器)是Struts 2个强有力工具有许多功能(feature)都是构建于它的上如国际化 、转换器 校验 等

什么是拦截器

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

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

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

实现原理

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

图1 拦截器<img src='/icons/84128diaoyong.gif' />序列图
图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文件摘取内容:

< erceptor name ="alias" ="com.opensymphony.xwork2.erceptor.AliasInterceptor" /> < erceptor name ="autowiring" ="com.opensymphony.xwork2.spring.erceptor.ActionAutowiringInterceptor" /> < erceptor name ="chain" ="com.opensymphony.xwork2.erceptor.ChainingInterceptor" /> < erceptor name ="conversionError" ="org.apache.struts2.erceptor.StrutsConversionErrorInterceptor" /> < erceptor name ="createSession" ="org.apache.struts2.erceptor.CreateSessionInterceptor" /> < erceptor name ="debugging" ="org.apache.struts2.erceptor.debugging.DebuggingInterceptor" /> < erceptor name ="external-ref" ="com.opensymphony.xwork2.erceptor.ExternalReferencesInterceptor" /> < erceptor name ="execAndWait" ="org.apache.struts2.erceptor.ExecuteAndWaitInterceptor" /> < erceptor name ="exception" ="com.opensymphony.xwork2.erceptor.ExceptionMappingInterceptor" /> < erceptor name ="fileUpload" ="org.apache.struts2.erceptor.FileUploadInterceptor" /> < erceptor name ="i18n" ="com.opensymphony.xwork2.erceptor.I18nInterceptor" /> < erceptor name ="logger" ="com.opensymphony.xwork2.erceptor.LoggingInterceptor" /> < erceptor name ="model-driven" ="com.opensymphony.xwork2.erceptor.ModelDrivenInterceptor" /> < erceptor name ="scoped-model-driven" ="com.opensymphony.xwork2.erceptor.ScopedModelDrivenInterceptor" /> < erceptor name ="params" ="com.opensymphony.xwork2.erceptor.ParametersInterceptor" /> < erceptor name ="prepare" ="com.opensymphony.xwork2.erceptor.PrepareInterceptor" /> < erceptor name ="-params" ="com.opensymphony.xwork2.erceptor.StaticParametersInterceptor" /> < erceptor name ="scope" ="org.apache.struts2.erceptor.ScopeInterceptor" /> < erceptor name ="servlet-config" ="org.apache.struts2.erceptor.ServletConfigInterceptor" /> < erceptor name ="sessionAutowiring" ="org.apache.struts2.spring.erceptor.SessionContextAutowiringInterceptor" /> < erceptor name ="timer" ="com.opensymphony.xwork2.erceptor.TimerInterceptor" /> < erceptor name ="token" ="org.apache.struts2.erceptor.TokenInterceptor" /> < erceptor name ="token-session" ="org.apache.struts2.erceptor.TokenSessionStoreInterceptor" /> < erceptor name ="validation" ="com.opensymphony.xwork2.validator.ValidationInterceptor" /> < erceptor name ="workflow" ="com.opensymphony.xwork2.erceptor.DefaultWorkflowInterceptor" /> < erceptor name ="store" ="org.apache.struts2.erceptor.MessageStoreInterceptor" /> < erceptor name ="checkbox" ="org.apache.struts2.erceptor.CheckboxInterceptor" /> < erceptor name ="profiling" ="org.apache.struts2.erceptor.ProfilingActivationInterceptor" />

配置和使用拦截器

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

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

package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public TimerInterceptorAction extends ActionSupport
标签:interceptor
0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: