zoukankan      html  css  js  c++  java
  • struts2内置拦截器和自定义拦截器详解(附源码)

    一、Struts2内置拦截器

           Struts2中内置类许多的拦截器,它们提供了许多Struts2的核心功能和可选的高级特 性。这些内置的拦截器在struts-default.xml中配置。只有配置了拦截器,拦截器才可以正常的工作和运行。Struts 2已经为您提供丰富多样的,功能齐全的拦截器实现。大家可以至struts2的jar包内的struts-default.xml查看关于默认的拦截器与 拦截器链的配置。内置拦截器虽然在struts2中都定义了,但是并不是都起作用的。因为并不是所有拦截器都被加到默认拦截器栈里了,只有被添加到默认拦 截器栈里的拦截器才起作用,看一下被加到默认拦截器栈的拦截器都有那些:struts2内置拦截器和自定义拦截器详解(附源码)
    五.开发自定义的拦截器步骤

        虽然,Struts 2为我们提供如此丰富的拦截器实现,但是在某种情况下并不能满足我们的需求,比如:访问控制的时候,在用户每次访问某个action时,我们要去校验用户 是否已经登入,如果没有登入我们将在action执行之前就被拦截,此时我们就需要自定义拦截器;下面我们具体看一下,如何实现自定义拦截器。

    1.实现拦截器类

    所有的Struts 2的拦截器都直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor。该接口提供了三

    个方法:

    1)     void init();在该拦截器被初始化之后,在该拦截器执行拦截之前,系统回调该方法。对于每个拦截器而言,此方法只执行一次。

    2)     void destroy();该方法跟init()方法对应。在拦截器实例被销毁之前,系统将回调该方法。

    3)     String intercept(ActionInvocation invocation) throws Exception;该方法是用户需要实现的拦截动作。该方法会返回一个字符串作为逻辑视图。

          除此之外,继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor是更简单的一种实现拦截器类的方式,因 为此类提供了init()和destroy()方法的空实现,这样我们只需要实现intercept方法。还有一种实现拦截器的方法是继承 MethodFilterInterceptor类,实现这个类可以实现局部拦截,即可以实现指定拦截某一个action的哪个方法,或者不拦截哪个方法

    2.注册自定义拦截器

    自定义拦截器类实现了,现在就要在struts.xml里注册这个拦截器;

    1).注册拦截器,在struts.xml中的package节点下注册拦截器

      <interceptors>

           <!-- name:拦截器的名称,class:自定义拦截器的类 -->

           <interceptorname="拦截器名称"class="自定义拦截器的class路径"/>

     </interceptors>
    2).使用拦截器,在需要使用自定义拦截器的action中定义如下代码

       <action>     

       <interceptor-refname="拦截器名称"/>

    </action>
    注意:因为struts2的很多功能都是根据Struts2默认拦截器栈实现的;如果此处只使用自定义的拦截器时,将失去struts2的很多核心功能;所以需要定义一个拦截器栈(由一个或多个拦截器组成)即此拦截器栈中一定要包括<interceptor-ref name="defaultStack"/>!!!!即如下:

    3)  拦截器栈
    <interceptor-stack name="拦截器栈的名称">

           <!--需要注意的是:系统默认的拦截器栈应要放在前面,在加入自定义拦截器; -->

              <interceptor-ref name="defaultState"/>

              <interceptor-ref name="自定义拦截器的名称"/>
    </interceptor-stack>
    4)  在action中使用栈
    <action>      

         <interceptor-refname="栈名称或拦截器名称"/>

          。。。。。

    </action>
    5) 如果此时需要所有的action都使用自定义拦截器或者拦截器栈时,此时就定义一个默认的拦截器或者拦截器栈,即全局的拦截器。

    在<default-interceptor-ref name="permissionStack"/>

    注意:如果在某个action中又使用了另一个拦截器,此时默认的拦截器将失效,为了确保能够使用默认的拦截器(包含Struts2的默认拦截器),又需要添加其他拦截器时,可以在action中加上其他拦截器

    面咱就以继承MethodFilterInterceptor类(它是AbstractInterceptor的子类)来实现一个权限控制的拦截器,别的页面都不展示了,在此,展示出拦截器类和struts.xml的配置:

    拦截器类AuthorInterceptor:

    package com.bzu.intecepter;

    import java.util.Map;

    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts2.StrutsStatics;

    import com.opensymphony.xwork2.ActionContext;

    import com.opensymphony.xwork2.ActionInvocation;

    import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;


    public class AuthorInterceptor extends MethodFilterInterceptor {

    @Override

    protected String doIntercept(ActionInvocation invocation) throws Exception {

    // TODO Auto-generated method stub

    ActionContext context = invocation.getInvocationContext();


    // 通过ActionContext来获取httpRequest

    HttpServletRequest request = (HttpServletRequest) context

    .get(StrutsStatics.HTTP_REQUEST);

    // 也可以通过ServletActionContext来获取httpRequest

    // HttpServletRequest request = ServletActionContext.getRequest();

    // 取得根目录的绝对路径

    String currentURL = request.getRequestURI();

    // 截取到访问的相对路径,可以通过这个和权限表比较来进行相应的权限控制

    String targetURL = currentURL.substring(currentURL.indexOf("/", 1),

    currentURL.length());

    System.out.println(currentURL + ".............." + targetURL);


    // 通过ActionContext获取session的信息,以Map形式返回

    Map session = context.getSession();

    // 获取容器里面的username值,如果存在说明该用户已经登录,让他执行操作,如果未登录让他进行登录

    String username = (String) session.get("username");

    System.out.println(username+"username");

    if (username != null) {

    invocation.invoke();

    }

    return "login";
    }
    }


    下面来看一下具体的struts.xml的配置:

    <!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

        "http://struts.apache.org/dtds/struts-2.0.dtd">

        <struts>
    <constant name="struts.i18n.encoding" value="utf-8" />
    <package name="struts2" extends="struts-default">
    <interceptors>

             <!-- 配置未登录进行操作的拦截器 -->

             <interceptor name="loginInterceptor" class="com.bzu.intecepter.AuthorInterceptor">

             <param name="excludeMethods">login</param>

             </interceptor>


             <!-- 重新封装一个默认的拦截器栈 -->

             <interceptor-stack name="myDefaultStack">

                   <interceptor-ref name="loginInterceptor" />

                   <interceptor-ref name="defaultStack" />

             </interceptor-stack>

         </interceptors>


         <!-- 为这个包设置默认的拦截器栈 -->

         <default-interceptor-ref name="myDefaultStack" />


        <global-results>

              <result name="login">/login.jsp</result>

         </global-results>

    <action name="LoginAction" class="com.bzu.action.LoginAction" method="login" >

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

    <result name="fail">fail.jsp</result>

    <result name="input">login.jsp</result>

    </action>

    </package>

    </struts>  


     

  • 相关阅读:
    Unix环境中的刷新
    C++ 的类型转换方法
    系统对信号的三种处理方式
    进程原语与线程原语的比较
    C和C++对带空参数列表的函数声明的不同处理
    函数指针
    字符串化的预处理器特征
    调试技巧
    信号产生的条件
    结构体大小问题
  • 原文地址:https://www.cnblogs.com/kabi/p/5182128.html
Copyright © 2011-2022 走看看