zoukankan      html  css  js  c++  java
  • 32.使用来MethodFilterInterceptor灵活拦截

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html

    步骤一、建立MethodAction,代码如下:
    package com.asm;

    import com.opensymphony.xwork2.ActionSupport;

    public class MethodAction extends ActionSupport{

           public String m1(){

                  return SUCCESS;

           }

           public String m2(){

                  return SUCCESS;

           }

           public String m3(){

                  return SUCCESS;

           }

    }

    步骤二、注册此Action,并为此Action配置拦截器。配置内容如下:
    <action name="*_*" class="com.asm.MethodAction" method="{2}">

                  <result name="success">/{2}Suc.jsp</result>

                  <interceptor-ref name="myMet">

                  </interceptor-ref>

    </action>

    我们为此Action配置了前面写的MyMethodFilterInterceptor拦截器,并在link.jsp中增加如下链接:
    <a href="<%=request.getContextPath()%>/Method_m1.action">m1</a><br>

    <a href="<%=request.getContextPath()%>/Method_m2.action">m2</a><br>

    <a href="<%=request.getContextPath()%>/Method_m3.action">m3</a><br>

    当点m1时会访问到m1Suc.jsp页面, 点m2、m3会分别访问到m2Suc.jsp、m3Suc.jsp页面。现在假如我们想访问m2、m3时不被拦截,我们只需修改MyMethodFilterInterceptor注册:修改内容为:
    <interceptor name="myMet"   class="com.asm.MyMethodFilterInterceptor">

                                <param name="excludeMethods">m2,m3</param>
    </interceptor>
    它的作用和增加<param name="includeMethods">m1</param>等价。上面是指定m2,m3方法调用时不被拦截,这里是指定只拦截m1。除了这种在注册拦截器时指定拦截外,还可以在引用拦截器时指定,即如下形式:
    <interceptor-ref name="myMet">

                                <param name="excludeMethods">m2,m3</param>

                                <param name="includeMethods">m1</param>
    </interceptor-ref>
    上面的两处<param>配置是等价的,但是如果〈param〉配置冲突,谁起作用?即如果我们对m1配置了excludeMethods同时又配置了includeMethods时,谁起作用,我们可以进行这些冲突的验证。以下是验证结果:
     引用配置(在Action引用拦截器时配置)时,以includeMethods的配置为准。一旦我们为拦截器使用了<param>配置,而对m1这样的方法不配置任何,就不会被拦截。但是如果不使用<param>,它们全部都要被拦截。
     注册配置时(在注册拦截器时配置),情况和“引用配置”完全一样。
     引用配置和注册配置冲突时,以引用配置为准。

  • 相关阅读:
    吴军博士《浪潮之巅》
    第十二周
    第十一周
    第十周
    第九周
    第四次作业
    第四周
    学习进度表
    世界是数字的
    第二阶段团队第八天成果。
  • 原文地址:https://www.cnblogs.com/sharpest/p/5587611.html
Copyright © 2011-2022 走看看