Struts2中的Interceptor拦截器

1.Interceptor简介

        struts2中的interceptor它是基于spring aop思想,而aop思想它本质上是通过动态代理来实现,strtus2的拦截器它主要是拦截Action的操作,在action的执行前或执行后进行一些其它的功能操作. 多个拦截器可以形成一个拦截器链,在访问它们时依次访问.

2.自定义Interceptor
    (1)定义Interceptor
           创建一个类来实现Interceptor接口,或继承MethodFilterInterceptor(只需实现doIntercept方法,较简洁);
           在doIntercept方法中实现拦截操作:
                 若满足条件,则实行invocation的invoke方法放行.
            若不满足,可以通过getAction方法获取被拦截的Action,如果这个action继承了ActionSupport类,就可以调用addActionError等方法.

              

    (2)在struts.xml文件中配置Intercepor

         格式如下图:   注:当引入自定义拦截器时,默认的defaultStack就不会导入,需要手动配置上去

                    此处是将defaultStack和两个自定义拦截器封装成了一个拦截器链myStack.

           

    (3)在Action的配置中引入配置好的拦截器

            <action name="login" class="com.action.UserAction" method="login">

                    <result  name="success">/index.jsp</result>

                    <interceptor-ref name="myStack"/>

            </action>

3.Interceptor实现只拦截指定的方法

        让Interceptor继承MethodFilterInterceptor类,在struts.xml配置时就可以指定要拦截的方法.

        <interceptors>

            <interceptor name="myInterceptor" class="com.utils.MyInterceptor">

                                             //includeMethods是固定参数,代表只会拦截后面写到的方法

                        <param name="includeMethods">show,add,login(填写需要拦截的方法)</param>

            </interceptor>

            <interceptor-stack name="myStack">

                      <interceptor-ref name="myInterceptor"/>

                      <interceptor-ref name="defaultStack"></interceptor-ref>

            </interceptor-stack>

        </interceptors>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值