拦截器interceptors基本介绍

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

拦截必须是无状态的,不能保证为每一个请求或者action创建一个实例.拦截可以选择短路一个action调用,然后返回一个结果码(如com.opensymphony.xwork.Action#SUCCESS);也可以选择在ActionInvocation#invoke()之前或者之后做一些处理。

Webwork & XWork Interceptors

拦截以key-value对的方式定义在xwork配置文件中.下面是定义在webwork-default.xml的拦截.如果您扩展webwork-default包,您就可以使用下面的拦截.否则您就必须在自己的包中的<interceptors>标签中定义name-class对.

Interceptor Name Description
Alias Interceptor alias 不同的request中的相似参数别名转换.
Chaining Interceptor chain 使前一个action中的属性在当前action中可用.一般和<result type="chain">一起使用 (在前一个action中).
Component Interceptor component 是Action中可以使用组件.和components.xml有关
Conversion Error Interceptor conversionError 把转型错误信息从ActionContext加到Action的field错误
Create Session Interceptor createSession 自动创建一个HttpSession对象,对于某些需要有HttpSession对象才能正常工作的拦截(如TokenInterceptor) 有用
Execute and Wait Interceptor execAndWait 后台执行action,发送给用户等待画面.
Exception Interceptor exception 把异常映射为结果.
File Upload Interceptor fileUpload 支持文件上传的拦截.更多信息参见javadoc
I18n Interceptor i18n 把所选的地域放入用户session
Logger Interceptor logger 输出Action的名字
Model Driven Interceptor model-driven 如果action实现了ModelDriven接口, 把getModel()的结果堆入valuestack.
Parameters Interceptor params 把request中的参数传入action.
Prepare Interceptor prepare 如果action实现了Preparable接口,调用其prepare()方法.
Scope Interceptor scope 把action的状态存在session或application范围内的简单方法
Servlet Config Interceptor servlet-config 可以直接访问HttpServletRequest和HttpServletResponse (谨慎使用,这样会使action与Servlet过于紧密)
Static Parameters Interceptor static-params 把定义在xwork.xml中的<action>标签下的<param>标签中的参数传入action
Timer Interceptor timer 输出action执行时间(包括内嵌拦截和视图)
Token Interceptor token 检查传到action中的token,防止多次提交
Token Session Interceptor token-session 功能同上,token储存在session中
Validation Interceptor validation 执行定义在xxxAction-validation.xml中的校验
Workflow Interceptor workflow 调用action类中的validate方法,如果产生错误返回INPUT画面.
Parameter Filter Interceptor N/A 根据合法的方法列表来去除一些参数

方法过滤

抽象的拦截可以通过指定included/excluded方法列表来实现可选择性

可以设置的参数如下:

  • excludeMethods - 被排除的方法
  • includeMethods - 被包含的方法

注意: 如果一个方法的名字同时出现在includeMethods和includeMethods里,它会被当作包含的方法。也就是,includeMethods优先于excludeMethods.

扩展了这一能力的拦截有:

  • TokenInterceptor
  • TokenSessionStoreInterceptor
  • DefaultWorkflowInterceptor
  • ValidationInterceptor

拦截参数覆盖

拦截的参数可以通过如下方式被覆盖

方法1:

<action name="myAction" class="myActionClass">
  <interceptor-ref name="exception"/>
    <interceptor-ref name="alias"/>
    <interceptor-ref name="params"/>
    <interceptor-ref name="servlet-config"/>
    <interceptor-ref name="prepare"/>
    <interceptor-ref name="i18n"/>
    <interceptor-ref name="chain"/>
    <interceptor-ref name="model-driven"/>
    <interceptor-ref name="fileUpload"/>
    <interceptor-ref name="static-params"/>
    <interceptor-ref name="params"/>
    <interceptor-ref name="conversionError"/>
    <interceptor-ref name="validation">
      <param name="excludeMethods">myValidationExcudeMethod</param>
    </interceptor-ref>
    <interceptor-ref name="workflow">
      <param name="excludeMethods">myWorkflowExcludeMethod</param>
    </interceptor-ref>
</action>

方法2:

<action name="myAction" class="myActionClass">
  <interceptor-ref name="defaultStack">
    <param name="validator.excludeMethods">myValidationExcludeMethod</param>
    <param name="workflow.excludeMethods">myWorkflowExcludeMethod</param>
  </interceptor-ref>
</action>

在第一个方法中,整个默认栈都被复制,然后根据需要改变参数。

在第二个方法中,我们引用了已经存在的拦截栈。在这个例子中这个栈是default-stack,然后覆盖了validator和workflow拦截的excludeMethods参数。注意在这个标签的name属性中有一个点(.),点之前的单词表示要被覆盖参数的拦截名,点之后的表示参数。形式如下:

<拦截名>.<参数名>

也要注意到,在这个例子中如果name属性用来表示一个拦截栈,就像指向一个拦截本身,那只能用上面描述的第一种方法.

拦截执行顺序

拦截提供了极好的方式去包装 前/后 处理.这种概念减少了代码重复(就像AOP).

<interceptor-stack name="xaStack">
  <interceptor-ref name="thisWillRunFirstInterceptor"/>
  <interceptor-ref name="thisWillRunNextInterceptor"/>
  <interceptor-ref name="followedByThisInterceptor"/>
  <interceptor-ref name="thisWillRunLastInterceptor"/>
</interceptor-stack>

注意一些拦截会打乱stack/chain/flow...所以顺序非常重要.

实现了com.opensymphony.xwork.interceptor.PreResultListener的拦截在Action之后Result之前执行.

executesthisWillRunFirstInterceptor
  thisWillRunNextInterceptor
    followedByThisInterceptor
      thisWillRunLastInterceptor
        MyAction1
        MyAction2 (chain)
        MyPreResultListener
        MyResult (result)
      thisWillRunLastInterceptor
    followedByThisInterceptor
  thisWillRunNextInterceptor
thisWillRunFirstInterceptor
 
 
 
   
   
   
   
Alias Interceptor (WebWork2文档中文化计划)
Chaining Interceptor (WebWork2文档中文化计划)
Component Interceptor (WebWork2文档中文化计划)
Conversion Error Interceptor (WebWork2文档中文化计划)
Create Session Interceptor (WebWork2文档中文化计划)
Exception Interceptor (WebWork2文档中文化计划)
Execute and Wait Interceptor (WebWork2文档中文化计划)
File Upload Interceptor (WebWork2文档中文化计划)
I18n Interceptor (WebWork2文档中文化计划)
Logger Interceptor (WebWork2文档中文化计划)
Model Driven Interceptor (WebWork2文档中文化计划)
Parameter Filter Interceptor (WebWork2文档中文化计划)
Parameters Interceptor (WebWork2文档中文化计划)
Prepare Interceptor (WebWork2文档中文化计划)
Scope Interceptor (WebWork2文档中文化计划)
Servlet Config Interceptor (WebWork2文档中文化计划)
Static Parameters Interceptor (WebWork2文档中文化计划)
Timer Interceptor (WebWork2文档中文化计划)
Token Interceptor (WebWork2文档中文化计划)
Token Session Interceptor (WebWork2文档中文化计划)
Validation Interceptor (WebWork2文档中文化计划)
Workflow Interceptor (WebWork2文档中文化计划)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值