zoukankan      html  css  js  c++  java
  • struts2的拦截器

    拦截器的作用:拦截器的方法在Action执行之前或者执行之后自动的执行,从而将通用的操作动态的插入到action执行的前后,这样用利于系统的解耦

    1:所有的拦截器的超级接口interceptor,Action去实现这个接口

    Interceptor有三个方法(init(),destory(),interceptor())

    Init()方法:在服务器启动的时候加载一次,并且只加载一次;

    Destroy()方法:当拦截器销毁时执行的方法;

    Interceptor()方法:其中里边有一个参数invocation

    package com.accp.intercepter;

    import com.opensymphony.xwork2.ActionContext;

    import com.opensymphony.xwork2.ActionInvocation;

    import com.opensymphony.xwork2.interceptor.Interceptor;

    public class LimitInterceptor implements Interceptor {

    public void destroy() {  }

    public void init() {}

    public String intercept(ActionInvocation invocation) throws Exception {

       System.out.println("-------------INTERCEPTOR---------------");

          //使用struts2的action上下文的对象获取会话,类似于servlet中的request对象

       Object user=ActionContext.getContext().getSession().get("user");

       //判断  

    if(user!=null){

        //执行action中的方法

        invocation.invoke();

       }   else   {

        ActionContext.getContext().put("message", "请先登录!");   

       return "login";

      }

       return null;

      }

    }

    Invocation.invoke()是如果只有一个拦截器执行完这个方法后,会返回给视图,如果有多个拦截器,它顺序的执行完所有的拦截器,才返回给视图

    2:可以在系统初始化中给拦截器指定默认的参数(也包括了定义拦截器方式)如下:

    在拦截器类中把hello当作属性set/get方法注入到拦截器类中;

    <!-- 定义拦截器 -->   <interceptors>    <interceptor name="LimitInterceptor" class="com.accp.intercepter.LimitInterceptor" >

      <!--指定系统初始化时给拦截器的参数-->

        <param name="hello">rosydawn</param>

       </>    <!-- 定义一个拦截器栈 -->    <interceptor-stack name="LimitInterceptorStack">     <!-- 引入struts的拦截器 -->     <interceptor-ref name="defaultStack" />     <!-- 引入自定义拦截器 -->     <interceptor-ref name="LimitInterceptor" />    </interceptor-stack>   </interceptors>   <!--    定义一个默认拦截器 一个包中只能定义一个默认拦截器, 如果在action中显示的引用一个拦截器,那么在这个包中定义的默认拦截器全部失效   -->   <default-interceptor-ref name="LimitInterceptorStack" /> <!--在Action中引入拦截栈-->

    <action name="*User" class="com.accp.action.UserAction" method="{1}">    <!-- 引入拦截器栈 -->     <interceptor-ref name="LimitInterceptorStack"/>    <result name="login">/login.jsp</result>    <result name="input">/login.jsp</result>   </action>

    <!--

    3.拦截器,拦截器栈,默认拦截器三者之间的关系

    (1)截器栈中可以包含多个拦截器,也可以包含多个拦截器栈

    (2)struts2中有一个默认的拦截器栈defaultStack,如果你手动引用了自己的拦截器栈,那么系统默认的拦截器栈将会失效,所有必须手动的引入系统的拦截器栈<interceptor-ref name="defaultStack"></interceptor>

    (3)如果想改变系统默认的拦截器栈,必须这样做<default-interceptor-ref name="LimitInterceptorStack"></defualt-interceptor-ref>

    指定系统初始化给拦截器的参数

    -->

     

                 

    <

    param

    name

    =

    "hello"

    >

    张钊钊

    </

    param

    点击链接加入群【java研究所】:http://jq.qq.com/?_wv=1027&k=eC5Q0O
  • 相关阅读:
    费马小定理
    Codeforces Round #448
    [Offer收割]编程练习赛37
    Educational Codeforces Round 33
    Codeforces Round #447
    Codeforces Round #446
    A Great Alchemist 最详细的解题报告
    A Mountaineer 最详细的解题报告
    hihoCoder 1040 矩阵判断 最详细的解题报告
    hihoCoder 1039 字符消除 最详细的解题报告
  • 原文地址:https://www.cnblogs.com/rosydawn/p/3408342.html
Copyright © 2011-2022 走看看