畢業(yè)設(shè)計(jì)JSP mvc外文翻譯_第1頁(yè)
畢業(yè)設(shè)計(jì)JSP mvc外文翻譯_第2頁(yè)
畢業(yè)設(shè)計(jì)JSP mvc外文翻譯_第3頁(yè)
畢業(yè)設(shè)計(jì)JSP mvc外文翻譯_第4頁(yè)
畢業(yè)設(shè)計(jì)JSP mvc外文翻譯_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Struts一種開(kāi)源MVC的實(shí)現(xiàn)這篇文章介紹 Struts,一個(gè)使用 servlet 和 JavaServer Pages 技術(shù)的一種 Model-View-Controller 的實(shí)現(xiàn)。Struts 可以幫助你控制 Web 項(xiàng)目中的變化并提高專業(yè)化。即使你可能永遠(yuǎn)不會(huì)用 Struts實(shí)現(xiàn)一個(gè)系統(tǒng),你可以獲得一些想法用于你未來(lái)的 servlet 和 JSP 網(wǎng)頁(yè)的實(shí)現(xiàn)中。簡(jiǎn)介在小學(xué)校園里的小孩子們都可以在因特網(wǎng)上發(fā)布 HTML 網(wǎng)頁(yè)。然而,有一個(gè)重大的不同在一個(gè)小學(xué)生和一個(gè)專業(yè)人士開(kāi)發(fā)的網(wǎng)站之間。網(wǎng)頁(yè)設(shè)計(jì)師(或者 HTML開(kāi)發(fā)人員)必須理解顏色、用戶、生產(chǎn)流程、網(wǎng)頁(yè)布局、瀏覽器兼容性、圖像創(chuàng)建

2、、JavaScript 等等。設(shè)計(jì)漂亮的網(wǎng)站需要做大量的工作,大多數(shù) Java 開(kāi)發(fā)人員更注重創(chuàng)建優(yōu)美的對(duì)象接口,而不是用戶界面。JavaServer Pages (JSP) 技術(shù)為網(wǎng)頁(yè)設(shè)計(jì)人員和 Java 開(kāi)發(fā)人員提供了一種聯(lián)系鈕帶。如果你開(kāi)發(fā)過(guò)大型 Web 應(yīng)用程序,你就理解“變化”這個(gè)詞語(yǔ)?!澳P?視圖-控制器”(MVC) 就是用來(lái)幫助你控制變化的一種設(shè)計(jì)模式。MVC 減弱了業(yè)務(wù)邏輯接口和數(shù)據(jù)接口之間的耦合。Struts 是一種 MVC 實(shí)現(xiàn),它將 Servlet 2.2 和 JSP 1.1 標(biāo)記(屬于 J2EE 規(guī)范)用作實(shí)現(xiàn)的一部分。你可能永遠(yuǎn)不會(huì)用 Struts 實(shí)現(xiàn)一個(gè)系統(tǒng),但了

3、解一下 Struts 或許使你能將其中的一些思想用于你以后的 Servlet和 JSP 實(shí)現(xiàn)中。模型-視圖-控制器 (MVC)JSP標(biāo)簽只解決了我們問(wèn)題中的一部分。我們依然有驗(yàn)證、流控制、以及更新應(yīng)用程序結(jié)構(gòu)的問(wèn)題。這就是MVC從哪兒來(lái)以及來(lái)干嘛的。MVC通過(guò)把問(wèn)題分成三類來(lái)幫助解決一些與單模塊相關(guān)的問(wèn)題:· Model(模型) 模塊包括應(yīng)用程序功能的核心。模型封裝著應(yīng)用程序的各個(gè)結(jié)構(gòu)。有時(shí)它所包含的唯一功能就是結(jié)構(gòu)。它對(duì)于視圖或者控制器一無(wú)所知。· View(視圖) 視圖提供了模型的演示。它是應(yīng)用程序的外表。視圖可以進(jìn)入模型獲得者,但是它對(duì)于設(shè)置者一無(wú)所知。除此之外,它對(duì)

4、于控制器也是一無(wú)所知。視圖僅僅當(dāng)模型發(fā)生改變的時(shí)候才被通知。 · Controller(控制器) 控制器對(duì)于用戶的輸入做出反應(yīng)。它創(chuàng)造和設(shè)置模型。 MVC 模型 2Web 給軟件開(kāi)發(fā)人員帶來(lái)了一些獨(dú)特的挑戰(zhàn),最顯著的就是客戶端和服務(wù)器端的無(wú)結(jié)構(gòu)連接。這種無(wú)結(jié)構(gòu)連接行為使得模型很難知道視圖的改變。在Web上,瀏覽器必須重復(fù)詢問(wèn)服務(wù)器端以此來(lái)發(fā)現(xiàn)應(yīng)用程序結(jié)構(gòu)的改變。另外一個(gè)顯而易見(jiàn)的改變就是相對(duì)于模型或者控制器,視圖采用了不同的技術(shù)。當(dāng)然,我們可以使用 Java(或者 PERL、C/C+ 或之前的其他代碼)代碼來(lái)生成 HTML。這種方法存在一些弊端:· Java 程序員應(yīng)該開(kāi)發(fā)

5、服務(wù),而不是 HTML。 · 布局的改變將需要改變代碼。 · 服務(wù)的客戶將有能力去創(chuàng)造一些頁(yè)面去滿足他們的一些特殊需求。 · 頁(yè)面設(shè)計(jì)人員將不能直接介入到頁(yè)面的開(kāi)發(fā)中。 · 嵌入在代碼中的HTML將會(huì)變得丑陋。 對(duì)于 Web,MVC的經(jīng)典形式將需要改變。圖4展示了MVC的Web適應(yīng),也就是通常所說(shuō)的MVC模型2或者M(jìn)VC 2。.圖 4. MVC 模型 2Struts,MVC 2 的一種實(shí)現(xiàn)Struts 是一組相互協(xié)作的類、servlet 和 JSP 標(biāo)記,它們組成一個(gè)可重用的 MVC 2 設(shè)計(jì)。這個(gè)定義表示 Struts 是一個(gè)框架,而不是一個(gè)庫(kù),但 S

6、truts 也包含了豐富的標(biāo)記庫(kù)和獨(dú)立于該框架工作的實(shí)用程序類。圖 5 顯示了 Struts 的一個(gè)概覽。圖 5. Struts 概覽Struts 概覽· 客戶端瀏覽器 一個(gè)來(lái)自客戶端瀏覽器的HTTP創(chuàng)建一個(gè)事件。Web 容器將會(huì)用一個(gè) HTTP 響應(yīng)來(lái)作出響應(yīng)。 · 控制器 控制器接收來(lái)自瀏覽器的請(qǐng)求,并決定發(fā)送請(qǐng)求到何處。就 Struts 而言,控制器就是一個(gè)以 servlet執(zhí)行的一個(gè)命令設(shè)計(jì)模式。 struts-config.xml 文件配置控制器。 · 業(yè)務(wù)邏輯 業(yè)務(wù)邏輯更新模型的狀態(tài),并幫助控制應(yīng)用程序的流。就 Struts 而言,這就是通過(guò)作為實(shí)際業(yè)

7、務(wù)邏輯“瘦”包裝的 Action 類完成的。 · 模型狀態(tài) 模型代表了應(yīng)用程序的狀態(tài)。業(yè)務(wù)對(duì)象更新應(yīng)用程序的狀態(tài)。ActionForm bean 在會(huì)話級(jí)或請(qǐng)求級(jí)表示模型的狀態(tài),而不是在持久級(jí)。JSP 文件使用 JSP 標(biāo)記讀取來(lái)自 ActionForm bean 的信息。 · 視圖 視圖就是一個(gè) JSP 文件。其中沒(méi)有流程邏輯,沒(méi)有業(yè)務(wù)邏輯,也沒(méi)有模型信息 - 只有標(biāo)記。標(biāo)記是使 Struts 有別于其他框架(如 Velocity)的因素之一。 Struts 詳細(xì)資料在圖6中展示了一個(gè)無(wú)其他附屬設(shè)備的阿帕奇struts的action包的UML圖表。圖 6 顯示了 Acti

8、onServlet (Controller)、 ActionForm (Form State) 和 Action (Model Wrapper) 之間的最小關(guān)系。 圖 6. 命令(ActionServlet) 與 模型 (Action & ActionForm) 之間的關(guān)系的 UML 圖ActionServlet 類 你還記得使用函數(shù)映射的日子嗎?你會(huì)映射一些輸入時(shí)間到一個(gè)函數(shù)的一個(gè)指針。如果你很老練,你可以把這些配置信息放進(jìn)一個(gè)文件里并且在運(yùn)行時(shí)加載該文件。函數(shù)指針裝扮了在C語(yǔ)言結(jié)構(gòu)化程序設(shè)計(jì)中的舊時(shí)光?,F(xiàn)在日子好過(guò)多了,自從我們有了Java技術(shù)、XML、J2EE等等之后。Strut

9、s控制器是一個(gè)映射事件(事件通常是一個(gè)HTTP post)到類的一個(gè) servlet。猜猜怎么著- 控制器用一個(gè)配置文件以致于你不必非硬編碼這些值。生活變了,但方法依然如此。ActionServlet 是 MVC 實(shí)現(xiàn)的命令部分并且它是框架的核心。 ActionServlet (Command) 創(chuàng)建并使用 Action 、 ActionForm 和 ActionForward 。正如前面所提及的, struts-config.xml 文件配置Command。在Web 工程創(chuàng)建期間, Action 和 ActionForm被擴(kuò)展用來(lái)解決特殊的問(wèn)題空間。文件 struts-config.xml

10、指導(dǎo) ActionServlet 如何擴(kuò)展這些類。這種方法有幾個(gè)優(yōu)點(diǎn): · 網(wǎng)頁(yè)設(shè)計(jì)人員不必費(fèi)力地通過(guò)Java代碼來(lái)理解應(yīng)用程序的流程。 · 當(dāng)流程發(fā)生改變時(shí)Java開(kāi)發(fā)人員不需要重新編譯代碼。 · 通過(guò)擴(kuò)展ActionServlet命令函數(shù)可以被添加進(jìn)來(lái)。 ActionForm 類 ActionForm 維持著Web應(yīng)用程序的會(huì)話狀態(tài)。 ActionForm 是一個(gè)必須為每個(gè)輸入表單模型創(chuàng)建該類的子類的抽象類。當(dāng)我說(shuō) 輸入表單模型 時(shí),我就是說(shuō) ActionForm 代表了一個(gè)由 HTML 表單設(shè)置或更新的一般意義上的數(shù)據(jù)。例如,你可能有一個(gè)由 HTML 表單設(shè)

11、置的 UserActionForm 。Struts 框架將會(huì): · 檢查 UserActionForm 是否存在;如果不存在,它將會(huì)創(chuàng)建該類的一個(gè)實(shí)例。 · Struts 將使用 HttpServletRequest 中相應(yīng)的域設(shè)置 UserActionForm 的狀態(tài)。沒(méi)有太多糟糕的請(qǐng)求.getParameter() 調(diào)用。例如,Struts 框架將從請(qǐng)求流中提取 fname 并調(diào)用 UserActionForm.setFname() 。 · Struts 框架在將在傳遞它到業(yè)務(wù)包裝 UserAction 之前將更新UserActionForm的狀態(tài)。 

12、3; 在傳遞它到 Action 類之前,Struts 將還會(huì)對(duì) UserActionForm 調(diào)用 validation() 方法進(jìn)行表單驗(yàn)證。 備注: 這樣做通常并不明智。別的網(wǎng)頁(yè)或業(yè)務(wù)對(duì)象可能有方法使用 UserActionForm ,然而驗(yàn)證可能不同。在 UserAction 類中進(jìn)行狀態(tài)驗(yàn)證可能更好。 · UserActionForm能夠維持一個(gè)會(huì)話級(jí)別 。 備注:· struts-config.xml 文件控制著 HTML 表單請(qǐng)求與 ActionForm 之間的映射。 · 多重請(qǐng)求會(huì)被映射到 UserActionForm 。 · UserAc

13、tionForm 可被映射到諸如向?qū)е惖亩嘀仨?yè)面的東西上。 Action 類 Action 類是一個(gè)圍繞業(yè)務(wù)邏輯的一個(gè)包裝器。 Action 類的目的就是將 HttpServletRequest 翻譯給業(yè)務(wù)邏輯。要使用 Action ,需重寫process() 原理。 ActionServlet (命令)通過(guò)使用 perform() 原理將參數(shù)化的類傳遞給 ActionForm 。此外,沒(méi)有太多討厭的 request.getParameter() 調(diào)用。通過(guò)事件到達(dá)這里的時(shí)間,輸入表單數(shù)據(jù)(或 HTML 表單數(shù)據(jù))已經(jīng)被從請(qǐng)求流中翻譯出來(lái)并進(jìn)入 ActionForm 類中。 注:擴(kuò)展 Act

14、ion 類時(shí)請(qǐng)注意簡(jiǎn)潔。 Action 類應(yīng)該控制應(yīng)用程序的流程,而不應(yīng)該控制應(yīng)用程序的邏輯。通過(guò)將業(yè)務(wù)邏輯放在單獨(dú)的包或 EJB 中,我們就可以提供更大的靈活性和可重用性。 考慮 Action 類的另一種方式是 Adapter 設(shè)計(jì)模式。 Action 的用途是“將類的接口轉(zhuǎn)換為客戶機(jī)所需的另一個(gè)接口。Adapter 使類能夠協(xié)同工作,如果沒(méi)有 Adapter,則這些類會(huì)因?yàn)椴患嫒莸慕涌诙鵁o(wú)法協(xié)同工作。”(摘自 Gof 所著的 Design Patterns - Elements of Reusable OO Software )。本例中的客戶機(jī)是 ActionServlet ,它對(duì)我們的具

15、體業(yè)務(wù)類接口一無(wú)所知。因此,Struts 提供了它能夠理解的一個(gè)業(yè)務(wù)接口,即 Action 。通過(guò)擴(kuò)展 Action ,我們使得我們的業(yè)務(wù)接口與 Struts 業(yè)務(wù)接口保持兼容。(一個(gè)有趣的發(fā)現(xiàn)是, Action 是類而不是接口)。 Action 開(kāi)始為一個(gè)接口,后來(lái)卻變成了一個(gè)類。真是金無(wú)足赤。) Error 類 UML 圖(圖 6)還包括 ActionError 和 ActionErrors 。 ActionError 封裝了單個(gè)錯(cuò)誤消息。 ActionErrors 是 ActionError 類的容器,View 可以使用標(biāo)記訪問(wèn)這些類。 ActionError 是 Struts 保持錯(cuò)誤

16、列表的方式。 圖 7. Command (ActionServlet) 與 Model (Action) 之間的關(guān)系的 UML 圖ActionMapping 類 輸入事件通常是在 HTTP 請(qǐng)求表單中發(fā)生的,servlet 容器將 HTTP 請(qǐng)求轉(zhuǎn)換為 HttpServletRequest ??刂破鞑榭摧斎胧录⒄?qǐng)求分派給某個(gè) Action 類。 struts-config.xml 確定 Controller 調(diào)用哪個(gè) Action 類。 struts-config.xml 配置信息被轉(zhuǎn)換為一組 ActionMapping ,而后者又被放入 ActionMappings 容器中。(你可能尚未

17、注意到這一點(diǎn),以 s結(jié)尾的類就是容器) ActionMapping 包含有關(guān)特定事件如何映射到特定 Action 的信息。 ActionServlet (Command) 通過(guò) perform() 方法將 ActionMapping 傳遞給 Action 類。這樣就使 Action 可訪問(wèn)用于控制流程的信息。 ActionMappings ActionMappings 是 ActionMapping 對(duì)象的一個(gè)集合。 Struts 的優(yōu)點(diǎn)· JSP 標(biāo)記機(jī)制的使用 標(biāo)記特性從 JSP 文件獲得可重用代碼和抽象 Java 代碼。這個(gè)特性能很好地集成到基于 JSP 的開(kāi)發(fā)工具中,這些工具

18、允許用標(biāo)記編寫代碼。 · 標(biāo)記庫(kù) 為什么要另發(fā)明一種輪子,或標(biāo)記庫(kù)呢?如果你在庫(kù)中找不到你所要的標(biāo)記,那就自己定義吧。此外,如果你正在學(xué)習(xí) JSP 標(biāo)記技術(shù),則 Struts 為你提供了一個(gè)起點(diǎn)。 · 開(kāi)放源碼 你可以獲得開(kāi)放源碼的全部?jī)?yōu)點(diǎn),比如可以查看代碼并讓使用庫(kù)的每個(gè)人檢查代碼。許多人都可以進(jìn)行很好的代碼檢查。 · MVC 實(shí)現(xiàn)樣例 如果你希望創(chuàng)建你自己的 MVC 實(shí)現(xiàn),則 Struts 可增加你的見(jiàn)識(shí)。 · 管理問(wèn)題空間 分治是解決問(wèn)題并使問(wèn)題可管理的極好方法。當(dāng)然,這是一把雙刃劍。問(wèn)題越來(lái)越復(fù)雜,并且需要越來(lái)越多的管理。 Struts 的缺點(diǎn)&

19、#183; 仍處于發(fā)展初期 Struts 開(kāi)發(fā)仍處于初級(jí)階段。他們正在向著發(fā)行版本 1.0 而努力,但與任何 1.0 版本一樣,它不可能盡善盡美。 · 仍在變化中 這個(gè)框架仍在快速變化。Struts 1.0 與 Struts 0.5 相比變化極大。為了避免使用不贊成使用的方法,你可能隔一天就需要下載最新的 Struts。在過(guò)去的 6 個(gè)月中,我目睹 Struts 庫(kù)從 90K 增大到 270K 以上。由于 Struts 中的變化,我不得不數(shù)次修改我的示例,但我不保證我的示例能與你下載的 Struts 協(xié)同工作。 · 正確的抽象級(jí)別 Struts 是否提供了正確的抽象級(jí)別?對(duì)

20、于網(wǎng)頁(yè)設(shè)計(jì)人員而言,什么是正確的抽象級(jí)別呢?這是一個(gè)用 $64K 的文字才能解釋清楚的問(wèn)題。在開(kāi)發(fā)網(wǎng)頁(yè)的過(guò)程中,我們是否應(yīng)該讓網(wǎng)頁(yè)設(shè)計(jì)人員訪問(wèn) Java 代碼?某些框架(如 Velocity)說(shuō)不應(yīng)該,但它提供了另一種 Web 開(kāi)發(fā)語(yǔ)言讓我們學(xué)習(xí)。在 UI 開(kāi)發(fā)中限制訪問(wèn) Java 有一定的合理性。最重要的是,如果讓網(wǎng)頁(yè)設(shè)計(jì)人員使用一點(diǎn) Java,他將使用大量的 Java。在 Microsoft ASP 的開(kāi)發(fā)中,我總是看到這樣的情況。在 ASP 開(kāi)發(fā)中,你應(yīng)該創(chuàng)建 COM 對(duì)象,然后編寫少量的 ASP 腳本將這些 COM 對(duì)象聯(lián)系起來(lái)。但是,ASP 開(kāi)發(fā)人員會(huì)瘋狂地使用 ASP 腳本。我會(huì)聽(tīng)

21、到這樣的話,“既然我可以用 VBScript 直接編寫 COM 對(duì)象,為什么還要等 COM 開(kāi)發(fā)人員來(lái)創(chuàng)建它呢?”通過(guò)使用標(biāo)記庫(kù),Struts 有助于限制 JSP 文件中所需的 Java 代碼的數(shù)量。Logic Tag 就是這樣的一種庫(kù),它對(duì)有條件地生成輸出進(jìn)行管理,但這并不能阻止 UI 開(kāi)發(fā)人員對(duì) Java 代碼的狂熱。無(wú)論你決定使用哪種類型的框架,你都應(yīng)該了解你要在其中部署和維護(hù)該框架的環(huán)境。當(dāng)然,這項(xiàng)任務(wù)真是說(shuō)起來(lái)容易做起來(lái)難。 · 有限的適用范圍 Struts 是一種基于 Web 的 MVC 解決方案,所以必須用 HTML、JSP 文件和 servlet 來(lái)實(shí)現(xiàn)它。 

22、3; J2EE 應(yīng)用程序支持 Struts 需要支持 JSP 1.1 和 Servlet 2.2 規(guī)范的 servlet 容器。僅憑這一點(diǎn)遠(yuǎn)不能解決你的全部安裝問(wèn)題,除非使用 Tomcat 3.2。我用 Netscape iPlanet 6.0 安裝這個(gè)庫(kù)時(shí)遇到一大堆問(wèn)題,按理說(shuō)它是第一種符合 J2EE 的應(yīng)用程序服務(wù)器。我建議你在遇到問(wèn)題時(shí)訪問(wèn) Struts 用戶郵件列表的歸檔資料。 · 復(fù)雜性 在將問(wèn)題分為幾個(gè)部分的同時(shí)也引入了復(fù)雜性。毫無(wú)疑問(wèn),要理解 Struts 必須接受一定的培訓(xùn)。隨著變化的不斷加入,這有時(shí)會(huì)令人很沮喪。歡迎訪問(wèn)本網(wǎng)站。 Struts 的前景在軟件開(kāi)發(fā)的新時(shí)

23、代事物都變化得很快。在最近5年里,我目睹過(guò)事物從 cgi/perl ,再到 ISAPI/NSAPI、再到使用 VB 的 ASP、一直到現(xiàn)在的 Java 和 J2EE 的變遷。Sun 正在努力地適應(yīng)JSP/servlet結(jié)構(gòu),就像他們通過(guò)Java語(yǔ)言和API在過(guò)去。你可以從 Sun 的網(wǎng)站獲得新的 JSP 1.2 和 Servlet 2.3 規(guī)范的草案。此外,一個(gè)標(biāo)準(zhǔn)的 JSP 標(biāo)記庫(kù)正在出現(xiàn)。 附件2:外文原文(復(fù)印件)出自IBM System Journal,2006,44(2):33-37 作者: Malcolm Davis StrutsAn Open-source MVC Impleme

24、ntationThis article introduces Struts, a Model-View-Controller implementation that uses servlets and JavaServer Pages (JSP) technology. Struts can help you control change in your Web project and promote specialization. Even if you never implement a system with Struts, you may get some ideas for your

25、 future servlets and JSP page implementationIntroductionKids in grade school put HTML pages on the Internet. However, there is a monumental difference between a grade school page and a professionally developed Web site. The page designer (or HTML developer) must understand colors, the customer, prod

26、uct flow, page layout, browser compatibility, image creation, JavaScript, and more. Putting a great looking site together takes a lot of work, and most Java developers are more interested in creating a great looking object interface than a user interface. JavaServer Pages (JSP) technology provides t

27、he glue between the page designer and the Java developer. If you have worked on a large-scale Web application, you understand the term change. Model-View-Controller (MVC) is a design pattern put together to help control change. MVC decouples interface from business logic and data. Struts is an MVC i

28、mplementation that uses Servlets 2.2 and JSP 1.1 tags, from the J2EE specifications, as part of the implementation. You may never implement a system with Struts, but looking at Struts may give you some ideas on your future Servlets and JSP implementations.Model-View-Controller (MVC)JSP tags solved o

29、nly part of our problem. We still have issues with validation, flow control, and updating the state of the application. This is where MVC comes to the rescue. MVC helps resolve some of the issues with the single module approach by dividing the problem into three categories: · Model The model co

30、ntains the core of the application's functionality. The model encapsulates the state of the application. Sometimes the only functionality it contains is state. It knows nothing about the view or controller. · View The view provides the presentation of the model. It is the look of the applic

31、ation. The view can access the model getters, but it has no knowledge of the setters. In addition, it knows nothing about the controller. The view should be notified when changes to the model occur. · Controller The controller reacts to the user input. It creates and sets the model. MVC Model 2

32、· The Web brought some unique challenges to software developers, most notably the stateless connection between the client and the server. This stateless behavior made it difficult for the model to notify the view of changes. On the Web, the browser has to re-query the server to discover modific

33、ation to the state of the application.· Another noticeable change is that the view uses different technology for implementation than the model or controller. Of course, we could use Java (or PERL, C/C+ or what ever) code to generate HTML. There are several disadvantages to that approach: ·

34、 Java programmers should develop services, not HTML. · Changes to layout would require changes to code. · Customers of the service should be able to create pages to meet their specific needs. · The page designer isn't able to have direct involvement in page development. · HTM

35、L embedded into code is ugly. · For the Web, the classical form of MVC needed to change. Figure 4 displays the Web adaptation of MVC, also commonly known as MVC Model 2 or MVC 2. ·Struts details· Displayed in Figure 6 is a stripped-down UML diagram of the org.apache.struts.action pack

36、age. Figure 6 shows the minimal relationships among ActionServlet (Controller), ActionForm (Form State), and Action (Model Wrapper). Figure 6. UML diagram of the relationship of the Command (ActionServlet) to the Model (Action & ActionForm)The ActionServlet class Do you remember the days of func

37、tion mappings? You would map some input event to a pointer to a function. If you where slick, you would place the configuration information into a file and load the file at run time. Function pointer arrays were the good old days of structured programming in C. · Life is better now that we have

38、 Java technology, XML, J2EE, and all that. The Struts Controller is a servlet that maps events (an event generally being an HTTP post) to classes. And guess what - the Controller uses a configuration file so you don_t have to hard-code the values. Life changes, but stays the same. · ActionServl

39、et is the Command part of the MVC implementation and is the core of the Framework. ActionServlet (Command) creates and uses Action, an ActionForm, and ActionForward. As mentioned earlier, the struts-config.xml file configures the Command. During the creation of the Web project, Action and ActionForm

40、 are extended to solve the specific problem space. The file struts-config.xml instructs ActionServlet on how to use the extended classes. There are several advantages to this approach: · The entire logical flow of the application is in a hierarchical text file. This makes it easier to view and

41、understand, especially with large applications. · The page designer does not have to wade through Java code to understand the flow of the application. · The Java developer does not need to recompile code when making flow changes. · Command functionality can be added by extending Actio

42、nServlet.The ActionForm class · ActionForm maintains the session state for the Web application. ActionForm is an abstract class that is sub-classed for each input form model. When I say input form model, I am saying ActionForm represents a general concept of data that is set or updated by a HTM

43、L form. For instance, you may have a UserActionForm that is set by an HTML Form. The Struts framework will: · Check to see if a UserActionForm exists; if not, it will create an instance of the class. · Struts will set the state of the UserActionForm using corresponding fields from the Http

44、ServletRequest. No more dreadful request.getParameter() calls. For instance, the Struts framework will take fname from request stream and call UserActionForm.setFname(). · The Struts framework updates the state of the UserActionForm before passing it to the business wrapper UserAction. · B

45、efore passing it to the Action class, Struts will also conduct form state validation by calling the validation() method on UserActionForm. Note: This is not always wise to do. There might be ways of using UserActionForm in other pages or business objects, where the validation might be different. Val

46、idation of the state might be better in the UserAction class. · The UserActionForm can be maintained at a session level. · Notes: · The struts-config.xml file controls which HTML form request maps to which ActionForm. · Multiple requests can be mapped UserActionForm. · UserA

47、ctionForm can be mapped over multiple pages for things such as wizards. The Action class The Action class is a wrapper around the business logic. The purpose of Action class is to translate the HttpServletRequest to the business logic. To use Action, subclass and overwrite the process() method. The

48、ActionServlet (Command) passes the parameterized classes to ActionForm using the perform() method. Again, no more dreadful request.getParameter() calls. By the time the event gets here, the input form data (or HTML form data) has already been translated out of the request stream and into an ActionFo

49、rm class.Figure 4. MVC Model 2Struts, an MVC 2 implementationStruts is a set of cooperating classes, servlets, and JSP tags that make up a reusable MVC 2 design. This definition implies that Struts is a framework, rather than a library, but Struts also contains an extensive tag library and utility c

50、lasses that work independently of the framework. Figure 5 displays an overview of Struts. Figure 5. Struts overviewStruts overview · Client browser An HTTP request from the client browser creates an event. The Web container will respond with an HTTP response. · Controller The Controller re

51、ceives the request from the browser, and makes the decision where to send the request. With Struts, the Controller is a command design pattern implemented as a servlet. The struts-config.xml file configures the Controller. · Business logic The business logic updates the state of the model and h

52、elps control the flow of the application. With Struts this is done with an Action class as a thin wrapper to the actual business logic. · Model state The model represents the state of the application. The business objects update the application state. ActionForm bean represents the Model state

53、at a session or request level, and not at a persistent level. The JSP file reads information from the ActionForm bean using JSP tags. · View The view is simply a JSP file. There is no flow logic, no business logic, and no model information - just tags. Tags are one of the things that make Strut

54、s unique compared to other frameworks like Velocity. Note: "Think thin" when extending the Action class. The Action class should control the flow and not the logic of the application. By placing the business logic in a separate package or EJB, we allow flexibility and reuse.Another way of

55、thinking about Action class is as the Adapter design pattern. The purpose of the Action is to "Convert the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn_t otherwise because of incompatibility interface" (from Design Patterns

56、- Elements of Reusable OO Software by Gof). The client in this instance is the ActionServlet that knows nothing about our specific business class interface. Therefore, Struts provides a business interface it does understand, Action. By extending the Action, we make our business interface compatible

57、with Struts business interface. (An interesting observation is that Action is a class and not an interface. Action started as an interface and changed into a class over time. Nothing's perfect.) The Error classes The UML diagram (Figure 6) also included ActionError and ActionErrors. ActionError

58、encapsulates an individual error message. ActionErrors is a container of ActionError classes that the View can access using tags. ActionErrors is Struts way of keeping up with a list of errors.Figure 7. UML diagram of the relationship of the Command (ActionServlet) to the Model (Action)The ActionMap

59、ping class An incoming event is normally in the form of an HTTP request, which the servlet Container turns into an HttpServletRequest. The Controller looks at the incoming event and dispatches the request to an Action class. The struts-config.xml determines what Action class the Controller calls. Th

60、e struts-config.xml configuration information is translated into a set of ActionMapping, which are put into container of ActionMappings. (If you have not noticed it, classes that end with s are containers)The ActionMapping contains the knowledge of how a specific event maps to specific Actions. The

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論