【正文】
您可以從 Sun 的網(wǎng)站獲得新的 JSP 和 Servlet 規(guī)范的草案。歡迎訪問本網(wǎng)站。我建議您在遇到問題時(shí)訪問 Struts 用戶郵件列表的歸檔資料(請(qǐng)參閱 參考資源 )。 大學(xué)畢業(yè)設(shè)計(jì)(論文) 第 21 頁 ? 有限的適用范圍 Struts 是一種基于 Web 的 MVC 解決方案,所以必須用 HTML、 JSP 文件和 servlet 來實(shí)現(xiàn)它。我會(huì)聽到這樣的話, “ 既然我可以用 VBScript 直接編寫 COM 對(duì)象,為什么還要等 COM 開發(fā)人員來創(chuàng)建它呢? ” 通過使用標(biāo)記庫, Struts 有助于限制 JSP 文件中所需的 Java 代碼的數(shù)量。最重要的是,如果讓網(wǎng)頁設(shè)計(jì)人員使用一點(diǎn) Java,他將使用大量的 Java。由于 Struts 中的變化,我不得不數(shù)次修改我的示例,但我不保證我的示例能與您下載的 Struts 協(xié)同工作。 ? 仍在變化中 這個(gè)框架仍在快速變化。當(dāng)然,這是一把雙刃劍。 ? 開放源碼 您可以獲得開放源碼的全部?jī)?yōu)點(diǎn),比如可以查看代碼并讓使用庫的每個(gè)人檢查代碼。 Struts 的優(yōu)點(diǎn) ? JSP 標(biāo)記機(jī)制的使用 標(biāo)記特性從 JSP 文件獲得可重用代碼和抽象 Java 代碼。(您可能尚未注意到這一點(diǎn),以 s結(jié)尾的類就是容器) 大學(xué)畢業(yè)設(shè)計(jì)(論文) 第 19 頁 ActionMapping 包含有關(guān)特定事件如何映射到特定 Action 的信息。 圖 7. Command (ActionServlet) 與 Model (Action) 之間的關(guān)系的 UML 圖 ActionMapping 類 輸入事件通常是在 HTTP 請(qǐng)求表單中發(fā)生的, servlet 容器將 HTTP 請(qǐng)求轉(zhuǎn)換為 HttpServletRequest 。) Error 類 UML 圖(圖 6)還包括 ActionError 和 ActionErrors 。通過擴(kuò)展 Action ,我們使得我們的業(yè)務(wù)接口與 Struts 業(yè)務(wù)接口保持兼容。 Adapter 使類能夠協(xié)同工作,如果沒有 Adapter,則這些類會(huì)因?yàn)椴患嫒莸慕涌诙鵁o法協(xié)同工作。 Action 類應(yīng)該控制應(yīng)用程序的流程,而不應(yīng)該控制應(yīng)用程序的邏輯。 ActionServlet (Command) 使用 perform() 方法將參數(shù)化的類傳遞給 ActionForm 。 ? UserActionForm 可跨多頁進(jìn)行映射,以執(zhí)行諸如向?qū)е惖牟僮?。? UserAction 類中進(jìn)行狀態(tài)驗(yàn)證可能更好。 ? Struts 框架在將 UserActionForm 傳遞給業(yè)務(wù)包裝 UserAction 之前將更新它的狀態(tài)。 Struts 框架將執(zhí)行以下操作: ? 檢查 UserActionForm 是否存在;如果不存在,它將創(chuàng)建該類的一個(gè)實(shí)例。 ActionForm 類 ActionForm 維護(hù) Web 應(yīng)用程序的會(huì)話狀態(tài)。這使得人們更容易查看和理解它,尤其是對(duì)于大型應(yīng)用程序而言。如前所述, 文件配置該 Command。正如您所料 控制器使用配置文件 以使您不必對(duì)這些值進(jìn)行硬編碼。如果您對(duì)此比較熟悉,您會(huì)將配置信息放入一個(gè)文件,并在運(yùn)行時(shí)加載這個(gè)文件。 詳細(xì)分析 Struts 圖 6 顯示的是 包的一個(gè)最簡(jiǎn) UML 圖。 JSP 文件使用 JSP 標(biāo)記讀取來自 ActionForm bean 的信息。就 Struts 而言,這是通過作為實(shí)際業(yè)務(wù)邏輯 “ 瘦 ” 包裝的 Action 類完成的。 ? Controller(控制器) 控制器接收來自瀏覽器的請(qǐng)求,并決定將這個(gè)請(qǐng)求發(fā)往何處。這個(gè)定義表示 Struts 是一個(gè)框架,而不是一個(gè)庫,但 Struts 也包含了豐富的標(biāo)記庫和獨(dú)立于該框架工作的實(shí)用程序類。 ? 嵌在代碼中的 HTML 很難看。這種方法有幾個(gè)缺點(diǎn): ? Java 程序員應(yīng)該開發(fā)服務(wù),而不是 HTML。這種無狀態(tài)行為使得模型很難將更改通知視圖。當(dāng)更改模型時(shí),視圖應(yīng)得到通知。 ? View(視圖) 視圖提供模型的表示。 MVC 通過將問題分為三個(gè)類別來幫助解決單一模塊方法所遇到的某些問題: ? Model(模型) 模型包含應(yīng)用程序的核心功能。盡管您可能永遠(yuǎn)不會(huì)用 Struts 實(shí)大學(xué)畢業(yè)設(shè)計(jì)(論文) 第 12 頁 現(xiàn)一個(gè)系統(tǒng),但了解一下 Struts 或許使您能將其中的一些思想用于您以后的 Servlet 的 JSP 實(shí)現(xiàn)中。 如果您開發(fā)過大型 Web 應(yīng)用程序,您就理解 變化 這個(gè)詞的含義。但是,小學(xué)生的網(wǎng)頁和專業(yè)開發(fā)的網(wǎng)站有質(zhì)的區(qū)別。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 大學(xué)畢業(yè)設(shè)計(jì)(論文) 第 7 頁 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 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 dispatches the request to an Action class. The determines what Action class the Controller calls. The 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 ActionServlet (Command) passes the ActionMapping to the Action class via the perform() method. This allows Action to access the information to control flow. ActionMappings ActionMappings is a collection of ActionMapping objects. Struts pros ? Use of JSP tag mechanism The tag feature promotes reusable code and abstracts Java code from the JSP 大學(xué)畢業(yè)設(shè)計(jì)(論文) 第 8 頁 file. This feature allows nice integration into JSPbased development tools that allow authoring with tags. ? Tag library Why reinvent the wheel, or a tag library? If you cannot find something you need in the library, contribute. In addition, Struts provides a starting point if you are learning JSP tag technology. ? Open source You have all the advantages of open source, such as being able to see the code and having everyone else using the library reviewing the code. Many eyes make for great code review. ? Sample MVC implementation Struts offers some insight if you want to create your own MVC implementation. ? Manage the problem space Divide and conquer is a nice way of solving the problem and making the problem manageable. Of course, the sword cuts both ways. The problem is more plex and needs more management. Struts cons ? Youth Struts development is still in preliminary form. They are working toward releasing a version , but as with any version, it does not provide all the bells and whistles. ? Change The framework is undergoing a rapid amount of change. A great deal of change has occurred between Struts and . You may want to download the most current Struts nightly distributions, to avoid deprecated method