struts的工作機制及分析

概述

本章講述Struts2的工作原理。

讀者如果曾經學習過Struts1.x或者有過Struts1.x的開發經驗,那麼千萬不要想當然地以爲這一章可以跳過。實際上Struts1.x與Struts2並無我們想象的血緣關係。雖然Struts2的開發小組極力保留Struts1.x的習慣,但因爲Struts2的核心設計完全改變,從思想到設計到工作流程,都有了很大的不同。

Struts2是Struts社區和WebWork社區的共同成果,我們甚至可以說,Struts2是WebWork的升級版,他採用的正是WebWork的核心,所以,Struts2並不是一個不成熟的產品,相反,構建在WebWork基礎之上的Struts2是一個運行穩定、性能優異、設計成熟的WEB框架。

本章主要對Struts的源代碼進行分析,因爲Struts2與WebWork的關係如此密不可分,因此,讀者需要下載xwork的源代碼,訪問http://www.opensymphony.com/xwork/download.action即可自行下載。

下載的Struts2源代碼文件是一個名叫struts-2.1.0-src.zip的壓縮包,裏面的目錄和文件非常多,讀者可以定位到struts-2.1.0-src"struts-2.0.10"src"core"src"main"java目錄下查看Struts2的源文件,如圖14所示。

(圖14)

主要的包和類

Struts2框架的正常運行,除了佔核心地位的xwork的支持以外,Struts2本身也提供了許多類,這些類被分門別類組織到不同的包中。從源代碼中發現,基本上每一個Struts2類都訪問了WebWork提供的功能,從而也可以看出Struts2與WebWork千絲萬縷的聯繫。但無論如何,Struts2的核心功能比如將請求委託給哪個Action處理都是由xwork完成的,Struts2只是在WebWork的基礎上做了適當的簡化、加強和封裝,並少量保留Struts1.x中的習慣。

以下是對各包的簡要說明:

包名

說明

org.apache.struts2. components

該包封裝視圖組件,Struts2在視圖組件上有了很大加強,不僅增加了組件的屬性個數,更新增了幾個非常有用的組件,如updownselect、doubleselect、datetimepicker、token、tree等。

另外,Struts2可視化視圖組件開始支持主題(theme),缺省情況下,使用自帶的缺省主題,如果要自定義頁面效果,需要將組件的theme屬性設置爲simple。

org.apache.struts2. config

該包定義與配置相關的接口和類。實際上,工程中的xml和properties文件的讀取和解析都是由WebWork完成的,Struts只做了少量的工作。

org.apache.struts2.dispatcher

Struts2的核心包,最重要的類都放在該包中。

org.apache.struts2.impl

該包只定義了3個類,他們是StrutsActionProxy、StrutsActionProxyFactory、StrutsObjectFactory,這三個類都是對xwork的擴展。

org.apache.struts2.interceptor

定義內置的截攔器。

org.apache.struts2.util

實用包。

org.apache.struts2.validators

只定義了一個類:DWRValidator。

org.apache.struts2.views

提供freemarker、jsp、velocity等不同類型的頁面呈現。

下表是對一些重要類的說明:

類名

說明

org.apache.struts2.dispatcher. Dispatcher

       該類有兩個作用:

       1、初始化

       2、調用指定的Action的execute()方法。

org.apache.struts2.dispatcher. FilterDispatcher

       這是一個過濾器。文檔中已明確說明,如果沒有經驗,配置時請將url-pattern的值設成/*。

       該類有四個作用:

       1、執行Action

       2、清理ActionContext,避免內存泄漏

       3、處理靜態內容(Serving static content)

       4、爲請求啓動xwork’s的截攔器鏈。

com.opensymphony.xwork2. ActionProxy

       Action的代理接口。

com.opensymphony.xwork2. ctionProxyFactory

       生產ActionProxy的工廠。

com.opensymphony.xwork2.ActionInvocation

       負責調用Action和截攔器。

com.opensymphony.xwork2.config.providers. XmlConfigurationProvider

       負責Struts2的配置文件的解析。

Struts2的工作機制

3.1Struts2體系結構圖

       Strut2的體系結構如圖15所示:

       (圖15)

3.2Struts2的工作機制

       從圖15可以看出,一個請求在Struts2框架中的處理大概分爲以下幾個步驟:

1、客戶端初始化一個指向Servlet容器(例如Tomcat)的請求;

2、這個請求經過一系列的過濾器(Filter)(這些過濾器中有一個叫做ActionContextCleanUp的可選過濾器,這個過濾器對於Struts2和其他框架的集成很有幫助,例如:SiteMesh Plugin);

3、接着FilterDispatcher被調用,FilterDispatcher詢問ActionMapper來決定這個請求是否需要調用某個Action;

4、如果ActionMapper決定需要調用某個Action,FilterDispatcher把請求的處理交給ActionProxy;

5、ActionProxy通過Configuration Manager詢問框架的配置文件,找到需要調用的Action類;

6、ActionProxy創建一個ActionInvocation的實例。

7、ActionInvocation實例使用命名模式來調用,在調用Action的過程前後,涉及到相關攔截器(Intercepter)的調用。

8、一旦Action執行完畢,ActionInvocation負責根據struts.xml中的配置找到對應的返回結果。返回結果通常是(但不總是,也可能是另外的一個Action鏈)一個需要被表示的JSP或者FreeMarker的模版。在表示的過程中可以使用Struts2 框架中繼承的標籤。在這個過程中需要涉及到ActionMapper。

注:以上步驟參考至網上,具體網址已忘記。在此表示感謝!

3.3Struts2源代碼分析

       和Struts1.x不同,Struts2的啓動是通過FilterDispatcher過濾器實現的。下面是該過濾器在web.xml文件中的配置:

代碼清單6:web.xml(截取)

    <filter>

       <filter-name>struts2</filter-name>

       <filter-class>

           org.apache.struts2.dispatcher.FilterDispatcher

       </filter-class>

    </filter>

    <filter-mapping>

       <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

    </filter-mapping>

       Struts2建議,在對Struts2的配置尚不熟悉的情況下,將url-pattern配置爲/*,這樣該過濾器將截攔所有請求。

       實際上,FilterDispatcher除了實現Filter接口以外,還實現了StrutsStatics接口,繼承代碼如下:

代碼清單7FilterDispatcher結構

publicclass FilterDispatcher implements StrutsStatics, Filter {

}

StrutsStatics並沒有定義業務方法,只定義了若干個常量。Struts2對常用的接口進行了重新封裝,比如HttpServletRequestHttpServletResponseHttpServletContext等。 以下是StrutsStatics的定義:

代碼清單8StrutsStatics.java

publicinterface StrutsStatics {

    /**

     *ConstantfortheHTTPrequestobject.

     */

    publicstaticfinal String HTTP_REQUEST ="com.opensymphony.xwork2.dispatcher.HttpServletRequest";

    /**

     *ConstantfortheHTTPresponseobject.

     */

    publicstaticfinal String HTTP_RESPONSE ="com.opensymphony.xwork2.dispatcher.HttpServletResponse";

    /**

     *ConstantforanHTTPrequest dispatcher}.

     */

    publicstaticfinal String SERVLET_DISPATCHER ="com.opensymphony.xwork2.dispatcher.ServletDispatcher";

    /**

     *Constantfortheservlet context}object.

     */

    publicstaticfinal String SERVLET_CONTEXT ="com.opensymphony.xwork2.dispatcher.ServletContext";

    /**

     *ConstantfortheJSPpage context}.

     */

publicstaticfinal String PAGE_CONTEXT ="com.opensymphony.xwork2.dispatcher.PageContext";

    /**ConstantforthePortletContextobject*/

    publicstaticfinal String STRUTS_PORTLET_CONTEXT = "struts.portlet.context";

}

    容器啓動後,FilterDispatcher被實例化,調用init(FilterConfig filterConfig)方法。該方法創建Dispatcher類的對象,並且將FilterDispatcher配置的初始化參數傳到對象中(詳情請參考代碼清單10),並負責Action的執行。然後得到參數packages,值得注意的是,還有另外三個固定的包和該參數進行拼接,分別是org.apache.struts2.statictemplate、和org.apache.struts2.interceptor.debugging,中間用空格隔開,經過解析將包名變成路徑後存儲到一個名叫pathPrefixes的數組中,這些目錄中的文件會被自動搜尋。

代碼清單9FilterDispatcher.init()方法

    publicvoid init(FilterConfig filterConfig) throws ServletException {

        this.filterConfig = filterConfig;      

        dispatcher = createDispatcher(filterConfig);

        dispatcher.init();      

        String param = filterConfig.getInitParameter("packages");

        String packages = "org.apache.struts2.static template org.apache.struts2.interceptor.debugging";

        if (param != null) {

            packages = param + " " + packages;

        }

        this.pathPrefixes = parse(packages);

}

代碼清單10FilterDispatcher.createDispatcher()方法

    protected Dispatcher createDispatcher(FilterConfig filterConfig) {

        Map<String,String> params = new HashMap<String,String>();

        for (Enumeration e = filterConfig.getInitParameterNames(); e.hasMoreElements(); ) {

            String name = (String) e.nextElement();

            String value = filterConfig.getInitParameter(name);

            params.put(name, value);

        }

        returnnew Dispatcher(filterConfig.getServletContext(), params);

   }

    當用戶向Struts2發送請求時,FilterDispatcherdoFilter()方法自動調用,這個方法非常關鍵。首先,Struts2對請求對象進行重新包裝,此次包裝根據請求內容的類型不同,返回不同的對象,如果爲multipart/form-data類型,則返回MultiPartRequestWrapper類型的對象,該對象服務於文件上傳,否則返回StrutsRequestWrapper類型的對象,MultiPartRequestWrapperStrutsRequestWrapper的子類,而這兩個類都是HttpServletRequest接口的實現。包裝請求對象如代碼清單11所示:

代碼清單11FilterDispatcher.prepareDispatcherAndWrapRequest()方法

protectedHttpServletRequest prepareDispatcherAndWrapRequest(

        HttpServletRequest request,

        HttpServletResponse response) throws ServletException {

        Dispatcher du = Dispatcher.getInstance();

        if (du == null) {

            Dispatcher.setInstance(dispatcher);         

            dispatcher.prepare(request, response);

        } else {

            dispatcher = du;

        }       

        try {

            request = dispatcher.wrapRequest(request, getServletContext());

        } catch (IOException e) {

            String message = "Could not wrap servlet request with MultipartRequestWrapper!";

            LOG.error(message, e);

            thrownew ServletException(message, e);

        }

        return request;

}

    request對象重新包裝後,通過ActionMappergetMapping()方法得到請求的ActionAction的配置信息存儲在ActionMapping對象中,該語句如下:mapping = actionMapper.getMapping(request, dispatcher.getConfigurationManager());。下面是ActionMapping接口的實現類DefaultActionMappergetMapping()方法的源代碼:

代碼清單12DefaultActionMapper.getMapping()方法

    public ActionMapping getMapping(HttpServletRequest request,

            ConfigurationManager configManager) {

        ActionMapping mapping = new ActionMapping();

        String uri = getUri(request);//得到請求路徑的URI,如:testAtcion.actiontestAction!method

        uri = dropExtension(uri);//刪除擴展名,默認擴展名爲action,在代碼中的定義是List extensions = new ArrayList() {{ add("action");}};

        if (uri == null) {

            returnnull;

        }

        parseNameAndNamespace(uri, mapping, configManager);//uri變量中解析出Actionnamenamespace

        handleSpecialParameters(request, mapping);//將請求參數中的重複項去掉

       //如果Actionname沒有解析出來,直接返回

        if (mapping.getName() == null) {

            returnnull;

        }

      

       //下面處理形如testAction!method格式的請求路徑

        if (allowDynamicMethodCalls) {

            // handle "name!method" convention.

            String name = mapping.getName();

            int exclamation = name.lastIndexOf("!");//!Action名稱和方法名的分隔符

            if (exclamation != -1) {

                mapping.setName(name.substring(0, exclamation));//提取左邊爲name

                mapping.setMethod(name.substring(exclamation + 1));//提取右邊的method

            }

        }

        return mapping;

    }

    該代碼的活動圖如下:

    

    (圖16

從代碼中看出,getMapping()方法返回ActionMapping類型的對象,該對象包含三個參數:Actionnamenamespace和要調用的方法method

   

    如果getMapping()方法返回ActionMapping對象爲null,則FilterDispatcher認爲用戶請求不是Action,自然另當別論,FilterDispatcher會做一件非常有意思的事:如果請求以/struts開頭,會自動查找在web.xml文件中配置的packages初始化參數,就像下面這樣(注意粗斜體部分)

代碼清單13web.xml(部分)

    <filter>

       <filter-name>struts2</filter-name>

       <filter-class>

           org.apache.struts2.dispatcher.FilterDispatcher

       </filter-class>

       <init-param>

           <param-name>packages</param-name>

           <param-value>com.lizanhong.action</param-value>

       </init-param>

    </filter>

    FilterDispatcher會將com.lizanhong.action包下的文件當作靜態資源處理,即直接在頁面上顯示文件內容,不過會忽略擴展名爲class的文件。比如在com.lizanhong.action包下有一個aaa.txt的文本文件,其內容爲“中華人民共和國”,訪問http://localhost:8081/Struts2Demo/struts/aaa.txt時會有如圖17的輸出:

(圖17

查找靜態資源的源代碼如清單14

代碼清單14FilterDispatcher.findStaticResource()方法

    protectedvoid findStaticResource(String name, HttpServletRequest request, HttpServletResponse response) throws IOException {

        if (!name.endsWith(".class")) {//忽略class文件

           //遍歷packages參數

            for (String pathPrefix : pathPrefixes) {

                InputStream is = findInputStream(name, pathPrefix);//讀取請求文件流

                if (is != null) {

                    ……(省略部分代碼)

                    // set the content-type header

                    String contentType = getContentType(name);//讀取內容類型

                    if (contentType != null) {

                        response.setContentType(contentType);//重新設置內容類型

                    }

                  ……(省略部分代碼)

                    try {

                     //將讀取到的文件流以每次複製4096個字節的方式循環輸出

                        copy(is, response.getOutputStream());

                    } finally {

                        is.close();

                    }

                    return;

                }

            }

        }

    }

    如果用戶請求的資源不是以/struts開頭——可能是.jsp文件,也可能是.html文件,則通過過濾器鏈繼續往下傳送,直到到達請求的資源爲止。

    如果getMapping()方法返回有效的ActionMapping對象,則被認爲正在請求某個Action,將調用Dispatcher.serviceAction(request, response, servletContext, mapping)方法,該方法是處理Action的關鍵所在。上述過程的源代碼如清單15所示。

代碼清單15FilterDispatcher.doFilter()方法

    publicvoid doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;

        HttpServletResponse response = (HttpServletResponse) res;

        ServletContext servletContext = getServletContext();

        String timerKey = "FilterDispatcher_doFilter: ";

        try {

            UtilTimerStack.push(timerKey);

            request = prepareDispatcherAndWrapRequest(request, response);//重新包裝request

            ActionMapping mapping;

            try {

                mapping = actionMapper.getMapping(request, dispatcher.getConfigurationManager());//得到存儲Action信息的ActionMapping對象

            } catch (Exception ex) {

               ……(省略部分代碼)

                return;

            }

            if (mapping == null) {//如果mappingnull,則認爲不是請求Action資源

                 String resourcePath = RequestUtils.getServletPath(request);

                if ("".equals(resourcePath) && null != request.getPathInfo()) {

                    resourcePath = request.getPathInfo();

                }

              //如果請求的資源以/struts開頭,則當作靜態資源處理

                if (serveStatic && resourcePath.startsWith("/struts")) {

                    String name = resourcePath.substring("/struts".length());

                    findStaticResource(name, request, response);

                } else {

                    //否則,過濾器鏈繼續往下傳遞

                    chain.doFilter(request, response);

                }

                // The framework did its job here

                return;

            }

           //如果請求的資源是Action,則調用serviceAction方法。

            dispatcher.serviceAction(request, response, servletContext, mapping);

        } finally {

            try {

                ActionContextCleanUp.cleanUp(req);

            } finally {

                UtilTimerStack.pop(timerKey);

            }

        }

    }

   

    這段代碼的活動圖如圖18所示:

(圖18

    Dispatcher.serviceAction()方法中,先加載Struts2的配置文件,如果沒有人爲配置,則默認加載struts-default.xmlstruts-plugin.xmlstruts.xml,並且將配置信息保存在形如com.opensymphony.xwork2.config.entities.XxxxConfig的類中。

    com.opensymphony.xwork2.config.providers.XmlConfigurationProvider負責配置文件的讀取和解析, addAction()方法負責讀取<action>標籤,並將數據保存在ActionConfig中;addResultTypes()方法負責將<result-type>標籤轉化爲ResultTypeConfig對象;loadInterceptors()方法負責將<interceptor>標籤轉化爲InterceptorConfi對象;loadInterceptorStack()方法負責將<interceptor-ref>標籤轉化爲InterceptorStackConfig對象;loadInterceptorStacks()方法負責將<interceptor-stack>標籤轉化成InterceptorStackConfig對象。而上面的方法最終會被addPackage()方法調用,將所讀取到的數據彙集到PackageConfig對象中,細節請參考代碼清單16

代碼清單16XmlConfigurationProvider.addPackage()方法

    protected PackageConfig addPackage(Element packageElement) throwsConfigurationException {

        PackageConfig newPackage = buildPackageContext(packageElement);

        if (newPackage.isNeedsRefresh()) {

            return newPackage;

        }

        if (LOG.isDebugEnabled()) {

            LOG.debug("Loaded " + newPackage);

        }

        // add result types (and default result) to this package

        addResultTypes(newPackage, packageElement);

        // load the interceptors and interceptor stacks for this package

        loadInterceptors(newPackage, packageElement);

        // load the default interceptor reference for this package

        loadDefaultInterceptorRef(newPackage, packageElement);

        // load the default class ref for this package

        loadDefaultClassRef(newPackage, packageElement);

        // load the global result list for this package

        loadGlobalResults(newPackage, packageElement);

        // load the global exception handler list for this package

        loadGlobalExceptionMappings(newPackage, packageElement);

        // get actions

        NodeList actionList = packageElement.getElementsByTagName("action");

        for (int i = 0; i < actionList.getLength(); i++) {

            Element actionElement = (Element) actionList.item(i);

            addAction(actionElement, newPackage);

        }

        // load the default action reference for this package

        loadDefaultActionRef(newPackage, packageElement);

        configuration.addPackageConfig(newPackage.getName(), newPackage);

        return newPackage;

    }

   

    活動圖如圖19所示:

(圖19

    配置信息加載完成後,創建一個Action的代理對象——ActionProxy引用,實際上對Action的調用正是通過ActionProxy實現的,而ActionProxy又由ActionProxyFactory創建,ActionProxyFactory是創建ActionProxy的工廠。

注:ActionProxyActionProxyFactory都是接口,他們的默認實現類分別是DefaultActionProxyDefaultActionProxyFactory,位於com.opensymphony.xwork2包下。

    在這裏,我們絕對有必要介紹一下com.opensymphony.xwork2.DefaultActionInvocation類,該類是對ActionInvocation接口的默認實現,負責Action和截攔器的執行。

    DefaultActionInvocation類中,定義了invoke()方法,該方法實現了截攔器的遞歸調用和執行Actionexecute()方法。其中,遞歸調用截攔器的代碼如清單17所示:

代碼清單17:調用截攔器,DefaultActionInvocation.invoke()方法的部分代碼

       if (interceptors.hasNext()) {

              //從截攔器集合中取出當前的截攔器

               final InterceptorMapping interceptor = (InterceptorMapping)interceptors.next();

               UtilTimerStack.profile("interceptor: "+interceptor.getName(),

                      new UtilTimerStack.ProfilingBlock<String>() {

                         public String doProfiling() throws Exception {

                            //執行截攔器(Interceptor)接口中定義的intercept方法

                             resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);

                            returnnull;

                         }

               });

           }

    從代碼中似乎看不到截攔器的遞歸調用,其實是否遞歸完全取決於程序員對程序的控制,先來看一下Interceptor接口的定義:

代碼清單18Interceptor.java

publicinterface Interceptor extends Serializable {

    void destroy();

    void init();

    String intercept(ActionInvocation invocation) throws Exception;

}

    所有的截攔器必須實現intercept方法,而該方法的參數恰恰又是ActionInvocation,所以,如果在intercept方法中調用invocation.invoke(),代碼清單17會再次執行,從ActionIntercepor列表中找到下一個截攔器,依此遞歸。下面是一個自定義截攔器示例:

代碼清單19CustomIntercepter.java

publicclass CustomIntercepter extends AbstractInterceptor {

    @Override

    public String intercept(ActionInvocation actionInvocation) throws Exception

    {

       actionInvocation.invoke();

       return"李贊紅";

    }

}

    截攔器的調用活動圖如圖20所示:

(圖20

    如果截攔器全部執行完畢,則調用invokeActionOnly()方法執行ActioninvokeActionOnly()方法基本沒做什麼工作,只調用了invokeAction()方法。

    爲了執行Action,必須先創建該對象,該工作在DefaultActionInvocation的構造方法中調用init()方法早早完成。調用過程是:DefaultActionInvocation()->init()->createAction()。創建Action的代碼如下:

代碼清單20DefaultActionInvocation.createAction()方法

    protectedvoid createAction(Map contextMap) {

        try {

            action = objectFactory.buildAction(proxy.getActionName(), proxy.getNamespace(), proxy.getConfig(), contextMap);

        } catch (InstantiationException e) {

       ……異常代碼省略

        }

    }

    Action創建好後,輪到invokeAction()大顯身手了,該方法比較長,但關鍵語句實在很少,用心點看不會很難。

代碼清單20DefaultActionInvocation.invokeAction()方法

protected String invokeAction(Object action, ActionConfig actionConfig) throwsException {

    //獲取Action中定義的execute()方法名稱,實際上該方法是可以隨便定義的

        String methodName = proxy.getMethod();

        String timerKey = "invokeAction: "+proxy.getActionName();

        try {

            UtilTimerStack.push(timerKey);           

            Method method;

            try {

              //將方法名轉化成Method對象

                method = getAction().getClass().getMethod(methodName, newClass[0]);

            } catch (NoSuchMethodException e) {

                // hmm -- OK, try doXxx instead

                try {

                  //如果Method出錯,則嘗試在方法名前加do,再轉成Method對象

                    String altMethodName = "do" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);

                    method = getAction().getClass().getMethod(altMethodName, newClass[0]);

                } catch (NoSuchMethodException e1) {

                    // throw the original one

                    throw e;

                }

            }

           //執行方法

            Object methodResult = method.invoke(action, new Object[0]);

            //處理跳轉

        if (methodResult instanceof Result) {

                this.result = (Result) methodResult;

                returnnull;

            } else {

                return (String) methodResult;

            }

        } catch (NoSuchMethodException e) {

              ……省略異常代碼

        } finally {

            UtilTimerStack.pop(timerKey);

        }

    }

    剛纔使用了一段插述,我們繼續回到ActionProxy類。

    我們說Action的調用是通過ActionProxy實現的,其實就是調用了ActionProxy.execute()方法,而該方法又調用了ActionInvocation.invoke()方法。歸根到底,最後調用的是DefaultActionInvocation.invokeAction()方法。

    以下是調用關係圖:

    

    其中:

Ø         ActionProxy:管理Action的生命週期,它是設置和執行Action的起始點。

Ø         ActionInvocation:在ActionProxy層之下,它表示了Action的執行狀態。它持有Action實例和所有的Interceptor

    以下是serviceAction()方法的定義:

代碼清單21Dispatcher.serviceAction()方法

        publicvoid serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,

                              ActionMapping mapping) throws ServletException {

        Map<String, Object> extraContext = createContextMap(request, response, mapping, context);

        // 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);

        if (stack != null) {

            extraContext.put(ActionContext.VALUE_STACK, ValueStackFactory.getFactory().createValueStack(stack));

        }

        String timerKey = "Handling request from Dispatcher";

        try {

            UtilTimerStack.push(timerKey);

            String namespace = mapping.getNamespace();

            String name = mapping.getName();

            String method = mapping.getMethod();

            Configuration config = configurationManager.getConfiguration();

            ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(

                    namespace, name, extraContext, truefalse);

            proxy.setMethod(method);

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

            // if the ActionMapping says to go straight to a result, do it!

            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 (stack != null) {

                request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);

            }

        } catch (ConfigurationException e) {

            LOG.error("Could not find action or result", e);

            sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);

        } catch (Exception e) {

            thrownew ServletException(e);

        } finally {

            UtilTimerStack.pop(timerKey);

        }

    }

    最後,通過Result完成頁面的跳轉。

3.4 本小節總結

       總體來講,Struts2的工作機制比Struts1.x要複雜很多,但我們不得不佩服Struts和WebWork開發小組的功底,代碼如此優雅,甚至能夠感受看到兩個開發小組心神相通的默契。兩個字:佩服。

       以下是Struts2運行時調用方法的順序圖:

(圖21)

四、      總結

閱讀源代碼是一件非常辛苦的事,對讀者本身的要求也很高,一方面要有紮實的功底,另一方面要有超強的耐力和恆心。本章目的就是希望能幫助讀者理清一條思路,在必要的地方作出簡單的解釋,達到事半功倍的效果。

當然,筆者不可能爲讀者解釋所有類,這也不是我的初衷。Struts2+xwork一共有700餘類,除了爲讀者做到現在的這些,已無法再做更多的事情。讀者可以到Struts官方網站下載幫助文檔,慢慢閱讀和理解,相信會受益頗豐。

本章並不適合java語言初學者或者對java博大精深的思想理解不深的讀者閱讀,這其中涉及到太多的術語和類的使用,特別不要去鑽牛角尖,容易使自信心受損。基本搞清楚Struts2的使用之後,再回過頭來閱讀本章,對一些知識點和思想也許會有更深的體會。

如果讀者的java功底比較渾厚,而且對Struts2充滿興趣,但又沒太多時間研究,不妨仔細閱讀本章,再對照Struts的源代碼,希望對您有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章