【正文】
大學(xué)畢業(yè)設(shè)計(jì)(論文) 第 11 頁 附錄 B 外文譯文 (譯自: Malcolm Davis. Strutsan opensource MVC implementation [J]. IBM Systems Journal , 2020,44(2):3337.) Struts—— MVC 的一種開放源碼實(shí)現(xiàn) Malcolm Davis 本文介紹 Struts,它是使用 servlet 和 JavaServer Pages 技術(shù)的一種 ModelViewController 實(shí)現(xiàn)。t able to have direct involvement in page development. ? HTML 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 monly known as MVC Model 2 or MVC 2. Struts details Displayed in Figure 6 is a strippeddown UML diagram of the package. Figure 6 shows the minimal relationships 大學(xué)畢業(yè)設(shè)計(jì)(論文) 第 3 頁 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 amp。網(wǎng)頁設(shè)計(jì)人員(或者 HTML 開發(fā)人員)必須理解顏色、用戶、生產(chǎn)流程、網(wǎng)頁布局、瀏覽器兼容性、圖像創(chuàng)建和 JavaScript 等等。 模型 視圖 控制器 (MVC) JSP 標(biāo)記只解決了部分問題。它是應(yīng)用程序的 外觀 。在 Web 上,為了發(fā)現(xiàn)對(duì)應(yīng)用程序狀態(tài)的修改,瀏覽器必須重新查詢服務(wù)器。 對(duì)于 Web,需要修改標(biāo)準(zhǔn)的 MVC 形式。就 Struts 而言,控制器是以 servlet 實(shí)現(xiàn)的一個(gè)命令設(shè)計(jì)模式。 ? View(視圖) 視圖就是一個(gè) JSP 文件。函數(shù)指針數(shù)組曾經(jīng)是用 C 語言進(jìn)行結(jié)構(gòu)化編程的很好方法。在創(chuàng)建 Web 項(xiàng)目時(shí),您將擴(kuò)展 Action 和 ActionForm 來解決特定的問題。 ActionForm 是一個(gè)抽象類,必須為每個(gè)輸入表 單模型創(chuàng)建該類的子類。 ? 在將它傳遞給 Action 類之前, Struts 還會(huì)對(duì) UserActionForm 調(diào)用 validation() 方法進(jìn)行表單狀態(tài)驗(yàn)證。 Action 類 Action 類是業(yè)務(wù)邏輯的一個(gè)包裝。通過將業(yè)務(wù)邏輯放在單獨(dú)的包或 EJB 中,我們就可以提供更大的靈活性和可重用性。(一個(gè)有趣的發(fā)現(xiàn)是, Action 是類而不是接口)??刂破鞑榭摧斎胧录⒄?qǐng)求分派給某個(gè) Action 類。這個(gè)特性能很好地集成到基于 JSP 的開發(fā)工具中,這些工具允許用標(biāo)記編寫代碼。問題越來越復(fù)雜,并且需要越來越多的管理。 ? 正確的抽象級(jí)別 Struts 是否提供了正確的抽象級(jí)別?對(duì)于網(wǎng)頁設(shè)計(jì)人員而言,什么是正確的抽象級(jí)別呢?這是一個(gè)用 $64K 的文字才能解釋清楚的問題。 Logic Tag 就是這樣的一種庫,它對(duì)有條件地生成輸出進(jìn)行管理,但這并不能阻止 UI 開發(fā)人員對(duì) Java 代碼的狂熱。 ? 復(fù)雜性 在將問題分為幾個(gè)部分的同時(shí)也引入了復(fù)雜性。此外,一個(gè)標(biāo)準(zhǔn) JSP 標(biāo)記庫即將出現(xiàn) 。隨著變化的不斷加入,這有時(shí)會(huì)令人很沮喪。當(dāng)然,這項(xiàng)任務(wù)真是說起來容易做起來難。在 UI 開發(fā)中限制訪問 Java 有一定的合理性。他們正在向著發(fā)行版本 而努力,但與任何 版本一樣,它不可能盡善盡美。此外,如果您正在學(xué)習(xí) JSP 標(biāo)記技術(shù),則 Struts 為您提供了一個(gè)起點(diǎn)。 配置信息被轉(zhuǎn)換為一組 ActionMapping ,而后者又被放入 ActionMappings 容器中。真是金無足赤。 Action 的用途是 “ 將類的接口轉(zhuǎn)換為客戶機(jī)所需的另一個(gè)接口。要使用 Action , 請(qǐng)創(chuàng)建它的子類并覆蓋 process() 方法。別的大學(xué)畢業(yè)設(shè)計(jì)(論文) 第 17 頁 網(wǎng)頁或業(yè)務(wù)可能使用 UserActionForm ,在這些地方,驗(yàn)證可能有所不同。例如,您可能有一個(gè)由 HTML 表單設(shè)置的 UserActionForm 。這種方法有幾個(gè)優(yōu)點(diǎn): ? 應(yīng)用程序的整個(gè)邏輯流程都存儲(chǔ)在一個(gè)分層的文本文件中。 Struts 的控制器是將事件(事件通常是 HTTP post)映射到類的一個(gè) servlet。標(biāo)記是使 Struts 有別于其他框架(如 Velocity)的因素之一。 ? 業(yè)務(wù)邏輯 業(yè)務(wù)邏輯更新模型的狀態(tài),并幫助控制應(yīng)用程序的流程。 圖 4. MVC Model 2 Struts, MVC 2 的一種實(shí)現(xiàn) Struts 是一組相互協(xié)作的類、 servlet 和 JSP 標(biāo)記,它們組成一個(gè)可重用的 MVC 2 設(shè)計(jì)。當(dāng)然,我們可以使用 Java(或者 PERL、 C/C++ 或別的語言)代碼生成 HTML。此外,它對(duì)控制器一無所知。這正是 MVC 發(fā)揮作用的地方。 JavaServer Pages (JSP) 技術(shù)為網(wǎng)頁設(shè)計(jì)人員和 Java 開發(fā)人員提供了一種聯(lián)系鈕帶。 if not, it will create an instance of the class. ? Struts will set the state of the UserActionForm using corresponding fields from the HttpServletRequest. No more dreadful () calls. For instance, the Struts framework will take fname from request stream and call (). ? The Struts framework updates the state of the UserActionForm before passing it to the business wrapper UserAction. ? Before 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. Validation of the state might be better in the UserAction class. ? The UserActionForm can be maintained at a session level. Notes: ? The file controls which HTML form request maps to which ActionForm. ? Multiple requests can be mapped UserActionForm. ? UserActionForm 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 大學(xué)畢業(yè)設(shè)計(jì)(論文) 第 5 頁 is to translate the HttpServletRequest to the business logic. To use Action, subclass and overwrite the process() method. The ActionServlet (Command) passes the parameterized classes to ActionForm using the perform() method. Again, no more dreadful () 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 ActionForm class. Figure 4. MVC Model 2 Struts, an MVC 2 implementation Struts 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 classes that work independently of the framework. Figure 5 displays an overview of Struts. Figure 5. Struts overview Struts overview 大學(xué)畢業(yè)設(shè)計(jì)(論文) 第 6 頁 ? Client browser An HTTP request from the client browser creates an event. The Web container will respond with an HTTP response.