【正文】
es can handle the HTML. 14 Struts takes this one step further and delegates much of the flow control and data acquisition to Action objects. The application then needs only a single servlet to act as a traffic cop. All the real work is parceled out to the Actions and the Struts configuration objects. Like servlets, Actions are efficient, multithreaded singletons. A single Action object can be handling any number of requests at the same time, optimizing your server’s resources. To get the most use out of your Actions, the ActionMapping object is used as a decorator for the Action object. It gives the Action a URI, or several URIs, and a way to pass different configuration settings to an Action depending on which URI is called. In this chapter, we took a close look at the ActionMapping properties and explained each property’s role in the scheme of things. We also looked at extending the standard ActionMapping object with custom properties—just in case your scheme needs even more things. Table The default ActionMapping classes ActionMapping Description Defaults the scope property to session Defaults the scope property to request 206 CHAPTER 7 Designing with ActionMappings In chapter 8, the real fun begins. The configuration objects covered so far are mainly a support system. They help the controller match an ining request with a serverside operation. Now that we have the supporting players, let’s meet the Struts diva: the Action object. 15 進(jìn)入 ActionMapping Model 2 架構(gòu) (第 1章 )鼓勵(lì)在同一個(gè)應(yīng)用中使用 servlet和 JSP頁(yè)面。在 Model 2下,我們 從調(diào)用一個(gè) servlet開始。 Servlet 處理業(yè)務(wù)邏輯并將控制 轉(zhuǎn)到相應(yīng)的頁(yè)面來(lái)完成響應(yīng)。 web應(yīng)用部署描述符 () 讓我們可以映射一個(gè) URL模板給一個(gè) servlet。這可以是一個(gè)常用的模板格式,象 *.do, 或者特別的路徑 , 象 。某些應(yīng)用通過給每個(gè)業(yè)務(wù)操作映射一個(gè)Servlet來(lái)實(shí) 現(xiàn) Model 2。這種方法可以工作,但許多應(yīng)用也涉及到大量的業(yè)務(wù)操作。因?yàn)?servlet是多線 程的,實(shí)例化這么多 servlet 顯然不是使用服務(wù)器資源的最好方式。 Servlet設(shè)計(jì)來(lái)可以處理大 量的并行請(qǐng)求。簡(jiǎn)單的創(chuàng)建很多 servlet并沒有 什么性能優(yōu)勢(shì)。 Servlet的主要工作是和容器和 HTTP進(jìn)行交互。處理業(yè)務(wù)操作是 servlet代表其它組件干的 事情。 Struts 通過使 ActionServlet 代表其他對(duì)象的業(yè)務(wù)操作來(lái)做這些事情。用 servlet 接受 請(qǐng)求并路由到一個(gè)處理器(稱為前端 控制器模式 [Go3])。 當(dāng)然 , 簡(jiǎn)單的代表其他對(duì)象的業(yè)務(wù)操作并沒有解決映射 URI [W3C, URI] 到業(yè)務(wù)操作的問 題。我們和 Web瀏覽器進(jìn)行通信能使用的唯一途徑是 HTTP 請(qǐng)求和 URI。如何組織 URI 來(lái)觸 發(fā)業(yè)務(wù)操作是開發(fā) web 應(yīng)用的關(guān)鍵 部分。 同時(shí),在實(shí)際應(yīng)用中,許多業(yè)務(wù)操作都以相似的方式來(lái)進(jìn)行處理。因?yàn)? Java 是多線程 的,如果我們使用同一個(gè) Action 對(duì)象來(lái)處理相似的操作,就能更好的利用服務(wù)器資源。但 是要讓它能工作,我們可能需要向?qū)ο髠鬟f一些配置參數(shù),以便和每個(gè)操作一起使用。 所以,底線是什么?為以一種靈活和有效的方式實(shí)現(xiàn) Model 2,我們需要 ? 路由對(duì)業(yè)務(wù)操作的請(qǐng)求到一個(gè) servlet ? 決定哪個(gè)業(yè)務(wù)操作和請(qǐng)求相關(guān) 16 ? 裝入多線程的 helper 對(duì)象來(lái)處理業(yè)務(wù)操作 ? 將每個(gè)請(qǐng)求特定的配置細(xì)節(jié)傳遞給 helper對(duì)象。 這樣, ActionMapping出場(chǎng)了! ActionMapping bean ActionMapping () 描述了框架是如何處 理每一離散的業(yè)務(wù)操作 (或 action)。在 Struts中,每個(gè) ActionMapping 通過其 path 屬性和 一個(gè)特定的 URI 相關(guān)。 當(dāng)一個(gè) 請(qǐng)求到來(lái), ActionServlet 使用 path 屬性來(lái)選擇對(duì)應(yīng)的 ActionMapping。一系列 ActionMapping 對(duì)象被放在一個(gè) ActionMappings 集合之中 ()。原本 , ActionMapping 對(duì)象用來(lái)擴(kuò)展 Action 對(duì)象 而不是 Action 類。 當(dāng)和 Action使用時(shí), mapping 給了一個(gè)特定的 Action 對(duì)象一 些額外的職責(zé)和新的功能。所以本質(zhì)上來(lái)說(shuō), Action 是一個(gè)裝飾器( decorator [Go4])。通 過這種方式, ActionMapping 以自己的方式發(fā)展為一個(gè)對(duì)象,可以和 Action一起使用,也 可以不。 Struts In Action DEFINITION 裝飾器模式( decorator pattern)的意圖是為一個(gè)對(duì)象動(dòng)態(tài)的附加上額外的職責(zé)和功能。Decorator 在擴(kuò)展功能時(shí)提供了一個(gè)除子類化外的選擇。 [Go4]. ActionMapping通常通過 Struts 配置文件創(chuàng)建。更多信息,參見第 4章。 ActionMapping 目錄 ActionMapping將對(duì) Struts 應(yīng)用有效的業(yè)務(wù)邏輯進(jìn)行編目。當(dāng)一個(gè)請(qǐng)求到達(dá)時(shí), servlet 在 ActionMapping目錄中 查找條目,來(lái)調(diào)用相應(yīng)的 bean。 ActionServlet 使用 ActionMapping bean 17 來(lái)決定接下來(lái)該做什么。它也許需要將控制轉(zhuǎn)發(fā)到其他資源。或者也許它需要組裝并且校驗(yàn) 一個(gè) ActionForm bean。某些時(shí)候,它也許會(huì)將控制傳遞給一個(gè) Action 對(duì)象,并且當(dāng)Action 返 回時(shí), 它可能會(huì)查找和這個(gè) mapping相關(guān)的 ActionForward。 ActionMapping工作起來(lái)就像是 servlet的一個(gè)路由聯(lián)絡(luò)。取決于 mapping 如何被填充,可 能被傳遞到上述的任何地方。 ActionMapping表達(dá)了 Struts 應(yīng)用的核心設(shè)計(jì)。如果你想知道 一個(gè) Struts 應(yīng)用是如何工作的,可以從 ActionMapping開始著手。如果你想知道如何編寫一 個(gè)新的 Struts 應(yīng)用,也請(qǐng)從 ActionMapping開始。 Mapping處于每個(gè) Struts 應(yīng)用的絕對(duì)核心。 在本章,我們將詳細(xì)討論 ActionMapping 屬性,并展示他們?nèi)绾斡兄谠O(shè)計(jì) Struts 應(yīng)用的流 程。 vs 在 Struts , ActionMapping 子類化了 ActionConfig () 類,并因向后兼容添加了 API 方法。 ActionMapping 已經(jīng)不推薦,但后繼版本中類的層次還沒有決定。 當(dāng)前,我們引用 ActionMapping 類,但你應(yīng)該注意到在 Struts ,所有的 action 屬性實(shí)際上 都在 ActionConfig超類中有定義。 ActionMapping類可以工作在兩個(gè)版本下。 ActionMapping 屬性 表 描述了基本的屬性。當(dāng)和其他配置組件一起,開 發(fā)人員可以擴(kuò)展 ActionMapping to 提 供附加的屬性。 屬性 說(shuō)明 path 來(lái)自于請(qǐng)求的 URI路徑,用來(lái)選擇該 mapping。 (API mand) forward 上下文相關(guān)的資源路徑,通過一個(gè) forward服務(wù)這個(gè)請(qǐng)求。 18 實(shí)際上是 forward, include, type 屬性的一個(gè),必須標(biāo)明。 或 include 上下文相關(guān)的資源路徑,通過一個(gè) include服務(wù)這個(gè)請(qǐng)求。 實(shí)際上是 forward, include, type 屬性的一個(gè),必須標(biāo)明。 或 type 可選,表明一個(gè) ,在實(shí)例化這個(gè) mapping Struts In Action 時(shí)使用。 classname 該 Mapping使用的 Action 類的全限定名稱 name 與該 Mapping相關(guān)的 form bean的名稱,如果有。這不是類名稱。而是在 form bean 配置中 使用的邏輯名稱。 roles 可以存取該 mapping的安全角色列表 . scope 范圍(請(qǐng)求或會(huì)話)識(shí)別符,如果有,與該 mapping相關(guān)的 forma Bean在其中創(chuàng)建 validate 如果與該 mapping相關(guān)的 form bean的 validate 方法 (如果有 )要被調(diào)用,設(shè)置為 true. input 輸入表單的上下文相關(guān)的路徑,如果校驗(yàn)錯(cuò)誤,控制應(yīng)該被返回到該表單??梢允侨魏?: HTML, JSP, VM, 或者另一個(gè) ActionMapping。 parameter 通用配置參數(shù),用來(lái)向 ActionMapping選定的 Action傳遞額外的參數(shù)信息。 . attribute 請(qǐng)求 范圍或者會(huì)話 范圍的屬性名稱, form bean 在下面被訪問。如果它是 bean39。特定名稱 外的名稱。 prefix 用來(lái)匹配請(qǐng)求參數(shù)名稱到 form bean 屬性名稱的前綴,如果有 suffix 在組裝 Actionform bean屬性時(shí),用來(lái)匹配請(qǐng)求參數(shù)名稱的后綴,如果有。 19 unknown 如果該 mapping 要被配置為應(yīng)用的缺省 mapping(處理那些沒有被其它 mapping處理的 請(qǐng)求 ),可設(shè)置為 true。在一個(gè)應(yīng)用中僅有一個(gè) mapping 可定義為缺省 unknown mapping。 forwards(s) 該 mapping使用的 ActionForward,如果有 exception(s) 該 mapping使用的 ExceptionHandler如果有 表 ActionMapping 基本屬性 在接下來(lái)的一節(jié),我們將詳細(xì)討論每個(gè)屬性。 path屬性 ActionMapping URI,或者 path,對(duì)用戶來(lái)說(shuō)就象 web server上的一個(gè)文件。但實(shí)際上它并 不代表一個(gè)文件。它是一個(gè)對(duì) ActionMapping的虛擬引用。 因?yàn)樗┞督o其他系統(tǒng), path 并不真正是個(gè)邏輯名稱,就想那些我們?cè)贏ctionForward使 用的一樣。 path 可以包括反 斜杠和擴(kuò)展名 如果他引用到一個(gè)文件系統(tǒng) —但他們都只是一 個(gè)單一名稱的一部分。 ActionMapping自身是一個(gè) “平面的 ” 名稱空間,完全沒有一個(gè)內(nèi)部層次關(guān)系。 They just happen to 使用 the same characters that we are 使用 d to seeing in hierarchical 文件 systems. 當(dāng)然 , 這對(duì)處理 ActionMapping仍然是有幫助的,就象他們是一個(gè)層次關(guān)系或者在同一個(gè) 文 件夾 .下的相關(guān)命令組的一部分。唯一的限制是名稱必須匹配 于在應(yīng)用部署描述文件中為 ActionServlet指定的樣式。這通常是 /do/*或者 *.do,但是其他相似的樣式也可以使用。如果 你在一個(gè)團(tuán)隊(duì)環(huán)境中工作,不同的團(tuán)隊(duì)可能有不同的 ActionMapping 名稱空間使用。某些 人可能工作于 /customer Acti