zoukankan      html  css  js  c++  java
  • Struts2运行机制(MVC)的分析:


    C:(controller)控制器          M:(model)模型处理    V:(view)视图
    Struts 2 的运行过程:
        核心控制器是FilterDispatcher会过滤所有的请求,如果请求以 action结尾,该请求会转入框架处理。当框架获取*action请求后,根据*action的前半部分决定调用哪个业务逻辑组件。最后根据业务逻辑组件的处理信息决定转发到哪个视图;
        Struts2有三部分组成:核心控制器是FilterDispatcher,业务控制器和业务逻辑组件组件,其中核心控制器FilterDispatcher由Struts2提供,而业务逻辑控制器和业务逻辑组件要用户自己实现。

          核心控制器FilterDispatcher:负责拦截所有用户的请求,如果用户的请求以action结尾,该请求就会转入Struts2框架处理。
         业务控制器组件:实现Action类的实例(或者继承了ActionSupport的实例),该类经常能够返回一个字符串(逻辑视图<result>的execute方法,用来实现业务控制)。
         业务逻辑组件:一般由javaBean或者EJB实现;

    注意在Struts2中处理用户请求的并不是业务逻辑,而是Action代理:过程是这样的,在Struts2框架中有一系列的拦截器,这些拦截器将HttpServletRequest请求中的参数分析出来,传入Action中,并回调execute()方法来处理用户请求。

        当 所有的请求被拦截器拦截时:执行流程
    1,FilterDispatcher会将所有的请求转发给 ActionProxy(Action代理),Action代理会根据配置文件struts.xml决定转发给那个Action;
    2,在请求转发给Action的过程中,会经过一系列拦截器,这些拦截器负责将请求解析并转发给相应的Action。
    3,经过相应的Action的 execute()方法处理,会得到一个 视图名的结果集,根据结果结合相应的模版产生相应的输出流。
    4,输出流也可以经过一些列的拦截器后,传给浏览器。




    对于源码根据图的理解:在Struts2的doFilter()方法中,通过execute.executeAction(request, response, mapping)执行进入了Dispatcher,生成了Dispatcher的对象,并调用了serviceAction()方法;接下里调用了create()方法生成ActionProxy的对象proxy ,通过proxy调用了Actioninvaction的的invoke()方法,接下里,Actioninvaction调用interceptor()方法,然后再 继续调用invoke()方法返回,判断是否interceptor()调用完成,直到所有的interceptor()方法调用完成,就执行*Action所对应的execute().


    模拟Struts2 实现的全过程代码;

    public class Main
    {
    public static void main(String[] args)
    {
                    
    new ActionInvaction().invoke();
    }
    }

    public interface Interceptor
    {
    public void interceptor(ActionInvaction invaction);
    }

    public class FristInterceptor implements Interceptor
    {

    public void interceptor(ActionInvaction invaction)
    {
    System.out.println(1);
    invaction.invoke();
    System.out.println(-1);
    }

    }


    public class SecondInterceptor implements Interceptor
    {

    public void interceptor(ActionInvaction invaction)
    {
    System.out.println(2);
    invaction.invoke();
    System.out.println(-2);
    }

    }

    public class ActionInvaction
    {
    List<Interceptor> interceptors = new ArrayList<Interceptor>();
    int index = -1;
    Action a = new Action();

    public ActionInvaction()
    {
    this.interceptors.add(new FristInterceptor());
    this.interceptors.add(new SecondInterceptor());
    }
     
    public void invoke()
    {
    index++;
    if(index >= interceptors.size())
    {
    a.execute();
    }
    else
    {
     
    this.interceptors.get(index).interceptor(this);
    }
    }
    }

    public class Action
    {
    public void execute()
    {
    System.out.println("execute!!!");
    }
    }


  • 相关阅读:
    新增html元素的使用
    音频的使用和插入以及动态文字的使用
    创建表单以及表单元素的使用
    今天学习image在html中的应用
    Repeater里查找控件
    CSS 基础教程
    自然图像的颜色统计特征 Image Color Statistics
    [转]UE中使用正则表达式的一些技巧
    [转]SQL Server 的事务和锁
    [转]Amtura 商务智能项目实现手记
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3151357.html
Copyright © 2011-2022 走看看