Struts2 拦截器技术笔记

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

在strust2中自定义拦截器可以

1、实现接口Interceptor (implements Interceptor);

2、继承抽象类AbstractInterceptor (extends AbstractInterceptor );

3、继承MethodFilterInterceptor (extends MethodFilterInterceptor);

 

Interceptor 包括了接口包括了3个方法:
void destroy()

 

void init()
java.lang.String intercept(ActionInvocation invocation) throws java.lang.Exception

由于通常情况下init()和destroy()用不上所以structs 又提供了一个抽象方法AbstractInterceptor。自定义拦截器继承AbstractInterceptor只需要重写intercept(ActionInvocation invocation)方法(AbstractInterceptor继承了Interceptor接口),不需要再关注void init()和void destroy()。

MethodFilterInterceptor 方法拦截器是继承自AbstractInterceptor。对AbstractInterceptor抽象类进行了扩展,主要增加了setExcludeMethods,getExcludeMethodsSet,setIncludeMethods,getIncludeMethodsSet。因此在structs.xml中配置

<interceptor-ref name="***">
            <param name="excludeMethods">***</param>
            <param name="includeMethods">***</param>
 </interceptor-ref>

这些自定义的拦截器要继承MethodFilterInterceptor,因为MethodFilterInterceptor中才有包含,排除的属性,不然会报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值