Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 205|回复: 0

[struts学习]Struts2讲义(9)继承方法拦截器的自定义拦截器配置

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-11 02:01:36 | 显示全部楼层 |阅读模式
    技术要点
       
    本节代码介绍方法拦截器配置并对缺省拦截器栈对整个Web项目的Action影响进行介绍。
       

       
         继承方法拦截器类的自定义拦截器类编写方式。
         
    配置文件struts.xml中如何定义方法拦截器和其属性。
         
    对所有Action配置拦截器和拦截器栈。
         演示代码
       

         
          
          
           java 代码
          
          
          
          <!------------------- 文件名:ExampleInterceptor.java----------------->   
          import com.opensymphony.xwork2.ActionInvocation;   
          import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;   
             
          public class ExampleInterceptor extends MethodFilterInterceptor {   
              //重写方法拦截器拦截方法   
              @Override   
              protected String doIntercept(ActionInvocation arg0) throws Exception {   
                  System.out.println("start invoking3...");   
                  String result = arg0.invoke();   
                  System.out.println("end invoking3...");   
                  return result;   
              }   
          
         
        <!-------------------文件名:ExampleInterceptor.java----------------->
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
    public class ExampleInterceptor extends MethodFilterInterceptor {
            //重写方法拦截器拦截方法
            @Override
            protected String doIntercept(ActionInvocation arg0) throws Exception {
                    System.out.println("start invoking3...");
                    String result = arg0.invoke();
                    System.out.println("end invoking3...");
                    return result;
            }[/code]
       
    LoginAction中增加了method方法
       

         
          
          
           Java 代码
          
          
          
          <!------------------------ 文件名:LoginAction.java------------------->   
          public String method()throws Exception {   
                  FORWARD = "success";   
                  return FORWARD;   
              }   
          
         
        <!------------------------文件名:LoginAction.java------------------->
    public String method()throws Exception {
                    FORWARD = "success";
                    return FORWARD;
            }[/code]
       
    拦截器映射配置。
       

         
          
          
           Java 代码
          
          
          
          <!-------------------- 文件名:struts.xml-------------->   
          <struts>   
              <!-- Action 所在包定义 -->   
              <package name="C04.3" extends="struts-default">   
              <!--  拦截器配置定义 -->   
                  <interceptors>   
                      <interceptor name="example"   
                          class="com.example.struts.interceptor.ExampleInterceptor">   
                      </interceptor>   
                  </interceptors>   
                  <!--     
                       缺省拦截器栈配置定义   
                      <default-interceptor-ref name="example"></default-interceptor-ref>   
                  -->   
                  <!-- Action 名字,类以及导航页面定义 -->   
                  <!--  通过Action类处理才导航的的Action定义 -->   
                  <action name="Login"   
                      class="com.example.struts.action.LoginAction" method="method">   
                      <result name="input">/jsp/login.jsp</result>   
                      <result name="success">/jsp/success.jsp</result>   
                      <!-- Action 方法拦截器配置定义 -->   
                      <interceptor-ref name="example">   
                          <!--  被拦截方法配置定义 -->   
                          <param name="includeMethods">method</param>   
                          <!--  不被拦截方法配置定义 -->   
                          <param name="excludeMethods">method,execute</param>   
                      </interceptor-ref>   
                  </action>   
              </package>   
          </struts>   
          
         
        <!--------------------文件名:struts.xml-------------->
    <struts>
            <!-- Action所在包定义 -->
            <package name="C04.3" extends="struts-default">
            <!-- 拦截器配置定义 -->
                    <interceptors>
                            <interceptor name="example"
                                    class="com.example.struts.interceptor.ExampleInterceptor">
                            </interceptor>
                    </interceptors>
                    <!--  
                            缺省拦截器栈配置定义
                            <default-interceptor-ref name="example"></default-interceptor-ref>
                    -->
                    <!-- Action名字,类以及导航页面定义 -->
                    <!-- 通过Action类处理才导航的的Action定义 -->
                    <action name="Login"
                            class="com.example.struts.action.LoginAction" method="method">
                            <result name="input">/jsp/login.jsp</result>
                            <result name="success">/jsp/success.jsp</result>
                            <!-- Action方法拦截器配置定义 -->
                            <interceptor-ref name="example">
                                    <!-- 被拦截方法配置定义 -->
                                    <param name="includeMethods">method</param>
                                    <!-- 不被拦截方法配置定义 -->
                                    <param name="excludeMethods">method,execute</param>
                            </interceptor-ref>
                    </action>
            </package>
    </struts>[/code]
       

        代码解释
       
    (1)ExampleInterceptor类中,继承MethodFilterInterceptor抽象类。读者也可以查看struts2的 源代码,在MethodFilterInterceptor中也只有一个抽象方法,但该抽象方法名为“doIntercept”。也对这个方法进行重写。 重写内容和4.3.1小节类似。
       

       
    (2)LoginAction.java中又定义了一个名为“method”方法,在struts.xml配置文件中,因为 LoginAction中有execute方法,又有method方法,因此在<Action>中,请读者注意struts.xml中黑体部 分,该部分代码表示现在LoginAction只执行method方法,而execute方法不被执行。笔者在<Action>中增加了一个 “method”属性,该属性中“=”后面的内容是Action中具体方法名,如果不写“method”属性,Action是缺省执行execute方 法。如果写了“method”属性,Action就执行“=”后写的具体方法。而不会执行execute方法。“example”拦截器还是如之前在<Action>前定义。在<Action>中配置“example”拦截器,笔者增加了“includeMethods”和 “excludeMethods”两个param属性定义。“includeMethods”表示的是被拦截器拦截的方法。方法名写 在<param>和</param>之间,如果有多个方法开发人员需要拦截器拦截,则方法名之间以“,”相隔。 “excludeMethods”表示的是不被拦截器拦截的方法。如果有多个方法,也是以“,”相隔。
       
    注意:如struts.xml配置文件中代码所示。假设
       
    <param name="excludeMethods">method,execute</param>
       
    这行代码被注释,则运行后method方法被拦截。如果 <param name="includeMethods">method</param>
       
    这行代码被注释,则MyEclipse的控制台中是没有任何拦截器拦截信息显示。说明method没有被拦截器拦截即拦截器没有执行。
       
    但是如struts.xml配置文件中代码显示,上述两行代码都没有被注释,读者有时候会不知道method方法到底是被拦截器拦截还是不被拦 截。其实method方法在两个属性中都被定义,Struts2认为method方法还是被拦截的。
       

       
    (3)请读者注意struts.xml配置文件中,<Action>前被注释的<default-interceptor- ref >定义。该标签表示的是所有Action都会执行的拦截器或拦截器栈的定义。之前的代码中对于拦截器的定义是在<Action>前,拦 截器的配置都是在<Action>中,比如“example”拦截器只有在LoginAction执行时候才会去拦截。如果是配 置<default-interceptor-ref >中,则不管是LoginAction还是其他struts.xml配置文件中定义的Action都会被“example”拦截。 在<default-interceptor-ref >中,也可以配置拦截器栈。如4.3.2小节中的“exampleStack”拦截器栈如果在<default-interceptor- ref >中配置,则所有Action执行时候,“exampleStack”拦截器栈都会执行该栈中包含的拦截器。
       

       
    注意:struts.xml配置文件中要么没有<default-interceptor-ref >定义,如果定义了也只能定义一次。该标签在struts.xml配置文件中只能写在<Action>前,而且只能写一次。不能重复定 义它。
       

       
    复制代码

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/11/020136281.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-17 12:44 , Processed in 0.424492 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表