zoukankan      html  css  js  c++  java
  • 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完成页面跳转。
  • 相关阅读:
    百度地图 导航
    设置cookies第二天0点过期
    GDI+一般性错误(A generic error occurred in GDI+)
    图片上传裁剪(asp.net)
    LeetCode_7. Reverse Integer
    LeetCode_1. Two Sum
    JAVA 基础编程练习题50 【程序 50 文件 IO】
    JAVA 基础编程练习题49 【程序 49 子串出现的个数】
    JAVA 基础编程练习题48 【程序 48 加密】
    JAVA 基础编程练习题47 【程序 47 打印星号】
  • 原文地址:https://www.cnblogs.com/xujian2014/p/4886539.html
Copyright © 2011-2022 走看看