zoukankan      html  css  js  c++  java
  • Struts(十)拦截器

    1.拦截器(interceptor):拦截器是Struts2的核心,Struts2的众多功能都是通过拦截器来实现的。与filter工作流程相似,只拦截action。

    2.Interceptor接口(继承)

    • void destory(); 很少使用
    • void init(); 很少使用
    • String intercept(ActionInvocation invocation)
    public String intercept(ActionInvocation arg0) throws Exception
        {
    
            String in = arg0.invoke();
                
            return in ;
        }

    3.ActionInvocation 接口

    • Object getAction();获得与这个ActionInvocation所关联到的action
    • String invoke(); 如果拦截器堆栈里有其他的拦截器,则将调用下一个拦截器;如果没有,则直接调用action
    • void addPreResultListener(PreResultListener listener);当action执行完与页面被渲染之前的这两个时间之间的

    4.PreResultListener接口

    • void beforeResult(ActionInvocation invocation,Strng resultCode)

    5.每个拦截器中的代码的执行顺序,在Action之前,拦截器的执行顺序与堆栈中定义的一致;

     而在Action和Result之后,拦截器的执行顺序与堆栈中定义的顺序相反。

    5.拦截器的配置

    • 编写实现interceptor接口的类
    • 在struts.xml文件中定义拦截器
    • 在action中使用拦截器
    <interceptors>
                <interceptor name="InterceptorMy" class="com.liule.interceptor.InterceptorMy"></interceptor>
    </interceptors>
        
    <action name="LoginAction">
                <result name="sucess">/servlet.jsp</result>
                <interceptor-ref name="InterceptorMy"></interceptor-ref>
    </action>
        

    6.在不明确声明拦截器的时候,调用默认的拦截器

    指定默认的拦截器栈:

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

    7.一旦定义了自己的拦截器,将其配置到action上后,我们需要在action的最后加上默认的拦截器栈:defaultStack

    <action name="LoginAction">
                <result name="sucess">/servlet.jsp</result>
                <interceptor-ref name="InterceptorMy"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </action>

    8.初始化参数

    <interceptors>
                <interceptor name="InterceptorMy" class="com.liule.interceptor.InterceptorMy">
                    <param name="hello">shengsiyuan</param>
                </interceptor>
            </interceptors>
    public class InterceptorMy implements Interceptor
    {
        private String hello;
    
        public String getHello()
        {
            return hello;
        }
    
        public void setHello(String hello)
        {
            this.hello = hello;
        }
    
        @Override
        public void destroy()
        {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void init()
        {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public String intercept(ActionInvocation arg0) throws Exception
        {
                
            
            String in = arg0.invoke();
                
            return in ;
        }
    
    }

    9.AbstractInterceptor 类(类似于适配器)

    • init();
    • destory();
    • intercept(ActionInvocation invocation);

    10.定义拦截器时可以直接继承AbstractInterceptor抽象类(该来实现了Interceptor接口,并且对init()和destory()方法进行了空实现),然后实现其抽象方法intercept即可。

    11.方法过滤拦截器(可以对指定方法进行拦截的拦截器)

    12.MethodFilterInterceptor 类,继承了AbstractInterceptor

    • String doIntercept(ActionInvocation invocation)

    13.参数:(拦截器是不是执行)

    • excludeMethods,不拦截的方法,拦截器不执行
    • includeMethods,拦截的方法,拦截器执行(优先级高)(默认)

    在方法过滤拦截器中,如果既没指定include参数与exclude参数,那么所有的方法都会拦截,也就是说所有的方法都默认为include。如果仅仅指定了include,那么只会拦截include中的方法,没有包含在include的不会被执行。

    14.Map map = invocation.getInvocationContext().getSession();

    15.Action.LOGIN 表示用户没有登录,让用户再登陆一下。

    http://www.cnblogs.com/liunanjava/p/4388647.html

  • 相关阅读:
    【剑指offer】判断二叉树是否为平衡二叉树
    【剑指offer】数字在排序数组中出现的次数
    八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)
    约瑟夫环问题-循环链表VS数组
    告别2014,你是否感谢这一年的自己?
    浅谈WEB页面提速(前端向)
    HTML5- Canvas入门(七)
    浅谈WEB安全性(前端向)
    是时候搁置Grunt,耍一耍gulp了
    前端神器avalonJS入门(二)
  • 原文地址:https://www.cnblogs.com/liu-Gray/p/4946749.html
Copyright © 2011-2022 走看看