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完成頁面跳轉。
好文要頂  關注我 收藏該文


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM