zoukankan      html  css  js  c++  java
  • Struts2拦截器说明

    有关于Struts2的拦截器的原理

    在此共设置了两个拦截器,firstInterception、SecondInterception

     1 package struts2_inteception;
     2 
     3 public class firstInterception implements Interception{
     4     public void interceptor(ActionInvocaton invocation){
     5         System.out.println("-1");
     6         invocation.invoke();
     7         System.out.println("1");
     8         
     9     }
    10 }
     1 package struts2_inteception;
     2 
     3 public class SecondInterception2 implements Interception{
     4     public void interceptor(ActionInvocaton invocation){
     5         System.out.println("-2");
     6         invocation.invoke();
     7         System.out.println("2");
     8         
     9     }
    10 }

    主函数Main类

    1 package struts2_inteception;
    2 
    3 public class Main {
    4     public static void main(String []args){
    5         new ActionInvocaton().invoke();
    6     }
    7 
    8 }

    拦截器接口Interceptor

    1 package struts2_inteception;
    2 
    3 public interface Interception {
    4     public void interceptor(ActionInvocaton actionInvocaton);
    5 }

    一个模拟struts2的Action类

     

    package struts2_inteception;
    
    public class Action {
        public void execute(){
            System.out.println("execute()方法的执行");
        }
    
    }

     

    一个ActionInvocation类,

     

    package struts2_inteception;
    
    import java.util.List;
    import java.util.ArrayList;
    
    
    public class ActionInvocaton {
        int index=-1;
        Action action=new Action();
        List<Interception> interceptions=new ArrayList<Interception>();
        public ActionInvocaton(){
            //在此调用一系列的拦截器
            this.interceptions.add(new firstInterception());
            this.interceptions.add(new SecondInterception2());
        }
        public void invoke(){
            index++;
            if (index>=interceptions.size()){
                //调用action的方法
                action.execute();
            }else{
                //调用拦截器中加的东西
                this.interceptions.get(index).interceptor(this);
            }
            
        }
        
        
    
    }

     真正的struts2的拦截器执行过程如下:

     

    执行的结果如下:

    -1
    -2
    execute()方法的执行
    2
    1
  • 相关阅读:
    数据结构 课程安排 (拓扑排序)
    数据结构 通畅工程 (最小生成树)
    01 C#基础
    计算机组成原理——第一章 系统概述
    数据结构——第八章 排序 第九章 文件
    数据结构——第七章 查找
    字符编码(转)
    数据结构——第六章 图
    NodeJS加密算法(转)
    入职总结
  • 原文地址:https://www.cnblogs.com/wbs19950305/p/8822994.html
Copyright © 2011-2022 走看看