Struts2(二)工作原理

一、概述

1、struts框架本身分为三个部分:核心控制器FilterDispatcher、业务控制器Action和用户实现的企业业务逻辑组件。

2、struts2工作的基本流程:

  • 客户端初始化一个指向Servlet容器的请求
  • org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter被调用,该过滤器询问ActionMaper这个请求是否需要调用某个Action
  • 如果ActionMapper决定需要调用某个Action,StrutsPrepareAndExecuteFilter把请求的处理交给ActionProxy
  • ActionProxy通过Configuration Manager询问框架的配置文件,找到需要调用的Action类
  • ActionProxy创建一个ActionProxy的实例
  • ActionProxy实例使用命名模式来调用
  • 一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果

二、源码分析

  下面解析org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter类

 1 public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter
 2 {
 3     /*
 4      * prepareOperation对象包含执行请求之前的准备工作
 5      * ExecuteOperations对象包含过滤器的执行操作
 6      */
 7     protected PrepareOperations prepare;
 8     protected ExecuteOperations execute;
 9     protected List<Pattern> excludedPatterns = null;
10
11     public void init(FilterConfig filterConfig) throws ServletException
12     {
13         //InitOperations类包含一些初始化操作
14         InitOperations init = new InitOperations();
15         Dispatcher dispatcher = null;
16         try
17         {
18             //封装filterConfig,其中有个主要方法getInitParameterNames将参数名字以String格式存储在List中
19             FilterHostConfig config = new FilterHostConfig(filterConfig);
20             //初始化struts内部日志
21             init.initLogging(config);
22             //创建dispatcher ,并初始化
23             dispatcher = init.initDispatcher(config);
24             init.initStaticContentLoader(config, dispatcher);
25             //初始化类属性:prepare 、execute
26             prepare = new PrepareOperations(dispatcher);
27             execute = new ExecuteOperations(dispatcher);
28             this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
29             //回调空的postInit方法
30             postInit(dispatcher, filterConfig);
31         }
32         finally
33         {
34             if (dispatcher != null) {
35                 dispatcher.cleanUpAfterInit();
36             }
37             init.cleanup();
38         }
39     }
40     /**
41      * Callback for post initialization
42      */
43     protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig)
44     {
45     }
46
47     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
48     {
49         HttpServletRequest request = (HttpServletRequest) req;
50         HttpServletResponse response = (HttpServletResponse) res;
51         try
52         {
53             if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns))
54             {
55                 //过滤器放行
56                 chain.doFilter(request, response);
57             }
58             else
59             {
60                 //设置编码国际化和地点
61                 prepare.setEncodingAndLocale(request, response);
62                 //创建action上下文
63                 //ActionContext是一个线程的本地变量,这意味着不同的action之间不会共享ActionContext,所以也不用考虑线程安全问题
64                 prepare.createActionContext(request, response);
65                 prepare.assignDispatcherToThread();
66                 request = prepare.wrapRequest(request);
67                 ActionMapping mapping = prepare.findActionMapping(request, response, true);
68                 //如果mapping为空,则认为不是调用action,会调用下一个过滤器链,直到获取到mapping才调用action
69                 if (mapping == null)
70                 {
71                     boolean handled = execute.executeStaticResourceRequest(request, response);
72                     if (!handled)
73                     {
74                         chain.doFilter(request, response);
75                     }
76                 }
77                 else
78                 {
79                     //执行action
80                     execute.executeAction(request, response, mapping);
81                 }
82             }
83         }
84         finally
85         {
86             prepare.cleanupRequest(request);
87         }
88     }
89     public void destroy() {
90         prepare.cleanupDispatcher();
91     }
92 }

  上述源码的第23行:dispatcher = init.initDispatcher(config);

//创建并初始化Dispatcher对象
    public Dispatcher initDispatcher( HostConfig filterConfig )   {
        Dispatcher dispatcher = createDispatcher(filterConfig);
        dispatcher.init();
        return dispatcher;
    }

  创建dispatcher,会读取 filterConfig 中的配置信息,将配置信息解析出来,封装成为一个Map,然后根绝servlet上下文和参数Map构造Dispatcher :

 private Dispatcher createDispatcher( HostConfig filterConfig )
 {
        Map<String, String> params = new HashMap<String, String>();
        for(Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); )
      {
            String name = (String) e.next();
            String value = filterConfig.getInitParameter(name);
            params.put(name, value);
         }
        return new Dispatcher(filterConfig.getServletContext(), params);
 }

  初始化dispatcher过程如下:

public void init()
{
    if (configurationManager == null)
        {
           configurationManager = createConfigurationManager(DefaultBeanSelectionProvider.DEFAULT_BEAN_NAME);
        }
         //初始化过程中,会加载一些配置文件,例如:default.properties,struts-default.xml,struts-plugin.xml,struts.xml等
       try
           {
           init_FileManager();
           init_DefaultProperties(); // [1]
           init_TraditionalXmlConfigurations(); // [2]
           init_LegacyStrutsProperties(); // [3]
           init_CustomConfigurationProviders(); // [5]
           init_FilterInitParameters() ; // [6]
           init_AliasStandardObjects() ; // [7]
           Container container = init_PreloadConfiguration();
           container.inject(this);
           init_CheckWebLogicWorkaround(container);
           if (!dispatcherListeners.isEmpty())
           {
               for (DispatcherListener l : dispatcherListeners)
               {
                   l.dispatcherInitialized(this);
               }
           }
           errorHandler.init(servletContext);
       }
       catch (Exception ex)
       {
           if (LOG.isErrorEnabled())
               LOG.error("Dispatcher initialization failed", ex);
           throw new StrutsException(ex);
       }
}

  上述分析的是StrutsPrepareAndExecuteFilter类的init方法,该方法在web容器启动的时候就会被调用,当用户访问某个action时,首先调用StrutsPrepareAndExecuteFilter类的doFilter方法,下面具体分析下这个方法:

  • 首先是设置编码格式和地点:

  prepare.setEncodingAndLocale(request, response);

  • 创建ActionContext,ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看作是一个容器(其实我们这里的容器就是一个Map而已),它存放的是Action在执行时需要用到的对象,比如Session、Application、Request、Locale、ValueStack等。

   prepare.createActionContext(request, response);

  • 分配调度到本地线程调度

    prepare.assignDispatcherToThread();

  • request进行包装,如果content_type是multipart/form-data类型,则将request包装成MultiPartRequestWrapper对象,否则包装成StrutsRequestWrapper对象
 request = prepare.wrapRequest(request);
public HttpServletRequest wrapRequest(HttpServletRequest request) throws IOException
{
    // don‘t wrap more than once
    if (request instanceof StrutsRequestWrapper)
    {
        return request;
    }
    String content_type = request.getContentType();
    if (content_type != null && content_type.contains("multipart/form-data"))
    {
        MultiPartRequest mpr = getMultiPartRequest();
        LocaleProvider provider = getContainer().getInstance(LocaleProvider.class);
        request = new MultiPartRequestWrapper(mpr, request, getSaveDir(), provider, disableRequestAttributeValueStackLookup);
    }
    else
    {
        request = new StrutsRequestWrapper(request, disableRequestAttributeValueStackLookup);
    }
    return request;
}
  • 然后通过ActionMapper的getMapping()方法得到请求的Action,Action的配置信息存储在ActionMapping对象中,

  ActionMapping mapping = prepare.findActionMapping(request, response, true);

  我们找到prepare对象的findActionMapping方法:

public ActionMapping findActionMapping(HttpServletRequest request, HttpServletResponse response, boolean forceLookup)
{
    //首先从request对象中取mapping对象,看是否存在
    ActionMapping mapping = (ActionMapping) request.getAttribute(STRUTS_ACTION_MAPPING_KEY); //struts.actionMapping
    //不存在就创建一个
    if (mapping == null || forceLookup)
    {
        try
        {
            //首先创建ActionMapper对象,通过ActionMapper对象创建mapping对象
            mapping = dispatcher.getContainer().getInstance(ActionMapper.class).getMapping(request, dispatcher.getConfigurationManager());
            if (mapping != null)
            {
                request.setAttribute(STRUTS_ACTION_MAPPING_KEY, mapping);
            }
        } catch (Exception ex) {
            dispatcher.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex);
        }
    }
    return mapping;
}

  ActionMapper接口的实现类DefaultActionMapper的getMapping()方法的源代码:

public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager)
{
          ActionMapping mapping = new ActionMapping();
          //获得请求的uri,即请求路径URL中工程名以后的部分,如/HelloWorld.action
          String uri = getUri(request);
          int indexOfSemicolon = uri.indexOf(";");
          uri = (indexOfSemicolon > -1) ? uri.substring(0, indexOfSemicolon) : uri;
          //删除扩展名,如.action或者.do
          uri = dropExtension(uri, mapping);
          if (uri == null)
          {
             return null;
          }
         //从uri中分离得到请求的action名、命名空间。
         parseNameAndNamespace(uri, mapping, configManager);
         //处理特殊的请求参数
         handleSpecialParameters(request, mapping);
         //如果允许动态方法调用,即形如/HelloWorldAction!getAll.action的请求,分离action名和方法名
         return parseActionName(mapping);
}
  • 如果mapping为空,则认为不是调用action,会调用下一个过滤器链,直到获取到mapping才调用action
if (mapping == null)
   {
      boolean handled = execute.executeStaticResourceRequest(request, response);
      if (!handled)
         {
            chain.doFilter(request, response);
         }
   } 
  • 如果mapping对象不为空,则会执行action

   execute.executeAction(request, response, mapping);

  其源码为:

public void serviceAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
        //封转上下文环境,主要将requestMap、params、session等Map封装成为一个上下文Map
        Map<String, Object> extraContext = createContextMap(request, response, mapping);
        // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
        ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
        boolean nullStack = stack == null;
        if (nullStack)
        {
            ActionContext ctx = ActionContext.getContext();
            if (ctx != null)
            {
                stack = ctx.getValueStack();
            }
        }
        if (stack != null)
        {
            extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
        }
        String timerKey = "Handling request from Dispatcher";
        try
        {
            UtilTimerStack.push(timerKey);
            String namespace = mapping.getNamespace();//从mapping对象获取命名空间
            String name = mapping.getName(); //获取请求的action名
            String method = mapping.getMethod(); //获取请求方法

          //根据执行上下文参数,命名空间,名称等创建用户自定义Action的代理对象
            ActionProxy proxy = getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
                    namespace, name, method, extraContext, true, false);

            request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());

          //如果配置文件中执行的这个action配置了result,就直接转到result
            if (mapping.getResult() != null)
            {
                Result result = mapping.getResult();
                result.execute(proxy.getInvocation());
            }
            else
            {
                proxy.execute();
            }

            // If there was a previous value stack then set it back onto the request
            if (!nullStack) {
                request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
            }
        }
        catch (ConfigurationException e)
        {
            logConfigurationException(request, e);
            sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);
        }
        catch (Exception e)
        {
            if (handleException || devMode)
            {
                sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
            }
            else
            {
                throw new ServletException(e);
            }
        }
        finally
        {
            UtilTimerStack.pop(timerKey);
        }
    }
}
  • 最后通过Result完成页面跳转。
时间: 2024-08-09 08:06:47

Struts2(二)工作原理的相关文章

菜鸟学Struts2——Struts工作原理

在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中需要用户实现的部分只有三个,那就是struts.xml,Action,Template(JSP),如下图: 2.3.31中的org.apache.struts2.dispatcher.ActionContextCleanUp已经被标记为@Deprecated Since Struts 2.1.3,2

struts2的工作原理与文件结构

struts2框架的工作原理: Struts2的文件详解: 1. web.xml 过滤器遇到.action后缀的请求就会拦截处理,当遇到.jsp .html等就会放行. 2. struts.xml <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd&qu

Struts2 的工作原理

Struts2 的工作原理: 1,把Action请求发送给 StrutsPrepareAndExecuteFilter StrutsPrepareAndExecuteFilter.doFilter() --> ExecuteOperations.executeAction() --> Dispatcher.serviceAction() --> 2,StrutsPrepareAndExecuteFilter把请求的处理交给 ActionProxy ActionProxy.execute(

HDSF主要节点讲解(二)工作原理

HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的.论文为GFS(Google File System)Google 文件系统(中文,英文). HDFS有很多特点: ① 保存多个副本,且提供容错机制,副本丢失或宕机自动恢复.默认存3份. ② 运行在廉价的机器上. ③ 适合大数据的处理.多大?多小?HDFS默认会将文件分割成block,64M为1个block.然后将block按键值对存储在HDFS上,并将键值对的

Struts2学习一----------Struts2的工作原理及HelloWorld简单实现

? 版权声明:本文为博主原创文章,转载请注明出处 Struts2工作原理 一个请求在Struts2框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2.这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的继承很有帮助,例如SiteMesh Plugin) 3.接着FilterDispatcher被调用,FilterDispatcher询问Action

从Struts2源码学习Struts2的工作原理

今天我和我好基友啊斌通过探讨struts2的源码,总结了一下它的原理,代码是不会骗人的. 总的来说:struts的工作原理有7步: 1 客户端初始化一个指向Servlet容器的请求: 2 这个请求经过一系列的过滤器 在项目部署的时候,由tomcat容器读取项目的web.xml文件,测试的web.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5&quo

struts2的工作原理

从图1我们不难看出struts2的工作流程大致分为一下几部分 1.客户端请求一个HttpServletRequest的请求,如在浏览器中输入http://localhost: 8080/bookcode/Reg.action就是提交一个(HttpServletRequest)请求. 2.这个请求经过一系列的过滤器(Filter)如(ActionContextCleanUp.其他过滤器(SiteMesh等). FilterDispatcher).注意:这里是有顺序的,先ActionContext

HDSF主要节点解说(二)工作原理

HDFS(Hadoop Distributed File System )Hadoop分布式文件系统. 是依据google发表的论文翻版的.论文为GFS(Google File System)Google 文件系统(中文.英文). HDFS有非常多特点: ① 保存多个副本,且提供容错机制,副本丢失或宕机自己主动恢复.默认存3份. ② 执行在便宜的机器上. ③ 适合大数据的处理. 多大?多小?HDFS默认会将文件切割成block,64M为1个block.然后将block按键值对存储在HDFS上,并

Struts2的工作原理(工作流程)

Struts2官方站点提供的Struts 2 的整体结构. 一个请求在Struts2框架中的处理大概分为以下几个步骤: 1.客户端提交一个HttpServletRequest请求(action或JSP页面). 2.这个请求被提交到一系列Filter过滤器,如ActionCleanUp和FilterDispatcher等. 3.FilterDispatcher是Struts2控制器的核心,它通常是过滤器链中的最后一个过滤器. 4.请求被发送到FilterDispatcher后,FilterDisp