【正文】
verview Struts 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 receives the request from the browser, and makes the decision where to send the request. With Struts, the Controller is a mand design pattern implemented as a servlet. The file configures the Controller. ? Business logic The business logic updates the state of the model and helps 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 at a session or request level, and not at a persistent level. The JSP file reads information from the ActionForm bean using JSP tags. 第 7頁 共 20頁 ? 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 Struts unique pared 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 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 inpatibility interface (from Design Patterns 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 patible 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. Nothing39。設(shè)計漂亮的網(wǎng)站需要做大量的工作,大多數(shù) Java 開發(fā)人員更注重創(chuàng)建優(yōu)美的對象接口,而不是用戶界面。視圖可以進入模型獲得者,但 第 13頁 共 20頁 是它對于設(shè)置者一無所知。 圖 4 展示了 MVC 的 Web 適應(yīng),也就是通常所說的 MVC 模型 2 或者 MVC 2。其中沒有流程邏輯,沒有業(yè)務(wù)邏輯,也沒有模型信息 只有標(biāo)記。文件 指導(dǎo) ActionServlet 如何擴展 這些 類。在 UserAction 類中進行狀態(tài)驗證可能更好。 Adapter 使類能夠協(xié)同工作,如果沒有 Adapter, 第 17頁 共 20頁 則這些類會因為不兼容的接口而無法協(xié)同工作。(你可能尚未注意到這一點,以 s 結(jié)尾的類就是容器) ActionMapping 包含 有關(guān)特定事件如何映射到特定 Action 的信息。 ? 仍在變化中 這個框架仍在快速變化。 ? 有限的適用范圍 Struts 是一種基于 Web 的 MVC 解決方案,所以必須用 HTML、 JSP 文件和 servlet 來實現(xiàn)它。 第 20頁 共 20頁 你可以從 Sun 的網(wǎng)站獲得新的 JSP 和 Servlet 規(guī)范的草案。我會聽到這樣的話, “既然我可以用 VBScript 直接編寫 COM 對象,為什么還要等 COM 開發(fā)人員來創(chuàng)建它呢? ”通過使用標(biāo)記庫, Struts 有助于限制 JSP 文件中所需的 Java 代碼的數(shù)量。當(dāng)然,這是一把雙刃劍。 圖 7. Command (ActionServlet) 與 Model (Action) 之間的關(guān)系的 UML 圖 ActionMapping 類 輸入事件通常是在 HTTP 請求表單中發(fā)生的, servlet 容器將 HTTP 請求轉(zhuǎn)換為 HttpServletRequest 。 Action 類應(yīng)該控制應(yīng)用程序的流程,而不應(yīng)該控制應(yīng)用程序的邏輯。 ? Struts 框架在將 在傳遞它到 業(yè)務(wù)包裝 UserAction 之前將更新UserActionForm 的狀態(tài) 。 ActionServlet 是 MVC 實現(xiàn)的 命令 部分 并且 它是框架的核心。業(yè)務(wù)對象更新應(yīng)用程序的狀態(tài)。 ? 服務(wù)的客戶將有能力去創(chuàng)造一些頁面去滿足他們的一些特殊需求 。有時它所包含的唯一功能就是結(jié)構(gòu)。即使你可能永遠不會用 Struts 實現(xiàn)一個系統(tǒng),你可以獲得一些想法用于你未來的 servlet 和 JSP 網(wǎng)頁的實現(xiàn)中 。 簡介 在小學(xué)校園里的小孩子們都可以在因特網(wǎng)上發(fā)布 HTML 網(wǎng)頁。它對于視圖或者控制器一無所知。 ? 頁面設(shè)計人員將不能直接介入到頁面的開發(fā)中 。 ActionForm bean 在會話級或請求級表示模型的狀態(tài),而不是在持久級。 ActionServlet (Command) 創(chuàng)建并使用 Action 、 ActionForm 和 ActionForward 。 ? 在傳遞它 到 Action 類之前, Struts 將 還會對 UserActionForm 調(diào)用 validation() 方法進行表單驗證。通過將業(yè)務(wù)邏輯放在單獨的包或 EJB 中,我們就可以提供更大的靈活性和可重用性??刂破鞑榭摧斎胧录⒄埱蠓峙山o某個 Action 類。問題 越來越復(fù)雜,并且需要越來越多的管理。 Logic Tag 就是這樣的一種庫,它對有條件地生成輸出進行管理,但這并不能阻止 UI 開發(fā)人員對 Java 代碼的狂熱。此外,一個標(biāo)準(zhǔn)的 JSP 標(biāo)記庫 正在出現(xiàn)。當(dāng)然,這項任務(wù)真是說起來容易做起來難。他們正在向著發(fā)行版本 而努力,但與任何 版本一樣,它不可能盡善盡美。 配置信息被轉(zhuǎn)換為一組 ActionMapping ,而后者又被放入 ActionMappings 容器中。 Action 的用途是 “將類的接口轉(zhuǎn)換為客戶機所需的另一個接口。別的網(wǎng)頁或業(yè)務(wù) 對象 可能 有方法 使用 UserActionForm , 然而驗證可能不同 。在 Web 工程 創(chuàng)建 期間 , Action 和 ActionForm 被 擴展 用 來解決 特殊 的問題 空間 。 ? 視圖 視圖就是一個 JSP 文件。 對于 Web, MVC 的經(jīng)典形式將需要改變 。它是應(yīng)用程序的外表。網(wǎng)頁設(shè)計師(或者 HTML開發(fā)人員)必須理解顏色、用戶、生產(chǎn)流程、網(wǎng)頁布局、瀏覽器兼容性、圖像創(chuàng)建、JavaScript 等等。s perfect.) The Error classes The UML diagram (Figure 6) also included ActionError and ActionErrors. ActionError 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 第 8頁 共 20頁 Model (Action) The ActionMapping class An ining event is normally in the form of an HTTP request, which the servlet Container turns into an HttpServletRequest. The Controller looks at the ining event and dispat