計算機外文翻譯StrutsMVC 的一種開放源碼實現(xiàn)
《計算機外文翻譯StrutsMVC 的一種開放源碼實現(xiàn)》由會員分享,可在線閱讀,更多相關《計算機外文翻譯StrutsMVC 的一種開放源碼實現(xiàn)(22頁珍藏版)》請在裝配圖網上搜索。
1、 外 文 翻 譯 學 院: 信息科學與工程學院 專 業(yè): 計算機科學與技術 學生姓名: 宋清云 學生班級: 計算機0801班 學生學號: 080405017 指導教師: 崔文成 指導教師評語 指導教師簽名: 年 月 日 Struts——an open
2、-source MVC implementation This 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 ge
3、t some ideas for your future servlets and JSP page implementation. Introduction Kids 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
4、colors, the customer, product 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 (
5、JSP) technology provides the 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
6、and data. Struts is an MVC implementation 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-Con
7、troller (MVC) JSP tags solved only 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 categ
8、ories: Model The model contains the core of the applications 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.
9、 It is the look of the application. 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 s
10、ets the model. MVC Model 2 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 ser
11、ver to discover modification 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 th
12、at approach: 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 isnt able to have direct involvement in page development. HTML embe
13、dded into code is ugly. For the Web, the classical form of MVC needed to change. Figure 1 displays the Web adaptation of MVC, also commonly known as MVC Model 2 or MVC 2. Figure 1. MVC Model 2 Struts, an MVC 2 implementation Struts is a set of cooperating classes, servlets, and JSP tags th
14、at 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 2 displays an overview of Struts. Figure 2. Struts view Struts
15、overview l Client browser An HTTP request from the client browser creates an event. The Web container will respond with an HTTP response. l Controller The Controller receives the request from the browser, and makes the decision where to send the request. With Struts, the Controller is a comma
16、nd design pattern implemented as a servlet. The struts-config.xml file configures the Controller. l 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 busines
17、s logic. l 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 t
18、ags. l 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 compared to other frameworks like Velocity. Struts details Displayed in Figure 3 is a stripped-down UML diagram of the
19、 org.apache.struts.action package. Figure 6 shows the minimal relationships among ActionServlet (Controller), ActionForm (Form State), and Action (Model Wrapper). Figure 3:the relationship between ActionServlet (Controller)、 ActionForm (Form State) and Action (Model Wrapper) The ActionServlet c
20、lass Do you remember the days of function 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.
21、 Life is better now that we have 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
22、stays the same. ActionServlet 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 p
23、roject, Action and ActionForm 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 mak
24、es it easier to view and 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 ext
25、ending ActionServlet. 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
26、 by a HTML 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 Htt
27、pServletRequest. 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. Before p
28、assing 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
29、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. UserActionForm can be mapped over
30、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 ActionServlet (Command)
31、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 ActionForm class. Note: "Think
32、 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
33、 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 - Elements of Reusable OO Software by Gof). The client in this instance is the Acti
34、onServlet 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 with Struts business interface. (An interesting observation is that Action is a cla
35、ss and not an interface. Action started as an interface and changed into a class over time. Nothings perfect.) The Error classes The UML diagram (Figure 4) also included ActionError and ActionErrors. ActionError encapsulates an individual error message. ActionErrors is a container of ActionError
36、classes that the View can access using tags. ActionErrors is Struts way of keeping up with a list of errors. Figure 4:the relationship between Command (ActionServlet) and Model (Action) The ActionMapping class An incoming event is normally in the form of an HTTP request, which the servlet Conta
37、iner 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. The struts-config.xml configuration information is translated into a set of ActionMapping, which a
38、re 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.
39、 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 file. This feature allows nice integration
40、into JSP-based development tools that allow authoring with tags. Tag library Why re-invent 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
41、 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 proble
42、m 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 complex and needs more management. Struts cons Youth Struts development is still in preliminary form. They are working toward releasing a
43、 version 1.0, but as with any 1.0 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 0.5 and 1.0. You may want to download the most current Struts nightly distributions, to avoid
44、 deprecated methods. In the last 6 months, I have seen the Struts library grow from 90K to over 270K. I had to modify my examples several times because of changes in Struts, and I am not going to guarantee my examples will work with the version of Struts you download. Correct level of abstraction
45、 Does Struts provide the correct level of abstraction? What is the proper level of abstraction for the page designer? That is the $64K question. Should we allow a page designer access to Java code in page development? Some frameworks like Velocity say no, and provide yet another language to learn f
46、or Web development. There is some validity to limiting Java code access in UI development. Most importantly, give a page designer a little bit of Java, and he will use a lot of Java. I saw this happen all the time in Microsoft ASP development. In ASP development, you were supposed to create COM obje
47、cts and then write a little ASP script to glue it all together. Instead, the ASP developers would go crazy with ASP script. I would hear "Why wait for a COM developer to create it when I can program it directly with VBScript?" Struts helps limit the amount of Java code required in a JSP file via tag
48、 libraries. One such library is the Logic Tag, which manages conditional generation of output, but this does not prevent the UI developer from going nuts with Java code. Whatever type of framework you decide to use, you should understand the environment in which you are deploying and maintaining the
49、 framework. Of course, this task is easier said than done. Limited scope Struts is a Web-based MVC solution that is meant be implemented with HTML, JSP files, and servlets. J2EE application support Struts requires a servlet container that supports JSP 1.1 and Servlet 2.2 specifications. This
50、 alone will not solve all your install issues, unless you are using Tomcat 3.2. Complexity Separating the problem into parts introduces complexity. There is no question that some education will have to go on to understand Struts. With the constant changes occurring, this can be frustrating at ti
51、mes. Future of Struts Things change rapidly in this new age of software development. In less than 5 years, we have seen things go from cgi/perl, to ISAPI/NSAPI, to ASP with VB, and now Java and J2EE. Sun is working hard to adapt changes to the JSP/servlet architecture, just as they have in the pa
52、st with the Java language and API. You can obtain drafts of the new JSP 1.2 and Servlet 2.3 specifications from the Sun Web site. Additionally, a standard tag library for JSP files is appearing. Struts——MVC 的一種開放源碼實現(xiàn) 本文介紹Struts,它是使用 servlet 和 JavaServer Pages 技術的一種 Model-View-Controller 實現(xiàn)。S
53、truts 可幫助您控制 Web 項目中的變化并提高專業(yè)化水平。盡管您可能永遠不會用 Struts 實現(xiàn)一個系統(tǒng),但您可以將其中的一些思想用于您以后的 servlet 和 JSP 網頁的實現(xiàn)中。 簡介 小學生也可以在因特網上發(fā)布 HTML 網頁。但是,小學生的網頁和專業(yè)開發(fā)的網站有質的區(qū)別。網頁設計人員(或者 HTML 開發(fā)人員)必須理解顏色、用戶、生產流程、網頁布局、瀏覽器兼容性、圖像創(chuàng)建和 JavaScript 等等。設計漂亮的網站需要做大量的工作,大多數(shù) Java 開發(fā)人員更注重創(chuàng)建優(yōu)美的對象接口,而不是用戶界面。JavaServer Pages (JSP) 技術為網頁設計人員和 J
54、ava 開發(fā)人員提供了一種聯(lián)系鈕帶。 如果您開發(fā)過大型 Web 應用程序,您就理解 變化 這個詞的含義。“模型-視圖-控制器”(MVC) 就是用來幫助您控制變化的一種設計模式。MVC 減弱了業(yè)務邏輯接口和數(shù)據(jù)接口之間的耦合。Struts 是一種 MVC 實現(xiàn),它將 Servlet 2.2 和 JSP 1.1 標記(屬于 J2EE 規(guī)范)用作實現(xiàn)的一部分。盡管您可能永遠不會用 Struts 實現(xiàn)一個系統(tǒng),但了解一下 Struts 或許使您能將其中的一些思想用于您以后的 Servlet 的 JSP 實現(xiàn)中。 模型-視圖-控制器 (MVC) JSP 標記只解決了部分問題。我們還得處理驗證、流程
55、控制和更新應用程序的狀態(tài)等問題。這正是 MVC 發(fā)揮作用的地方。MVC 通過將問題分為三個類別來幫助解決單一模塊方法所遇到的某些問題: l Model(模型) 模型包含應用程序的核心功能。模型封裝了應用程序的狀態(tài)。有時它包含的唯一功能就是狀態(tài)。它對視圖或控制器一無所知。 l View(視圖) 視圖提供模型的表示。它是應用程序的 外觀。視圖可以訪問模型的讀方法,但不能訪問寫方法。此外,它對控制器一無所知。當更改模型時,視圖應得到通知。 l Controller(控制器) 控制器對用戶的輸入作出反應。它創(chuàng)建并設置模型。 MVC Model 2 Web 向軟件開發(fā)人員提出了一些
56、特有的挑戰(zhàn),最明顯的就是客戶機和服務器的無狀態(tài)連接。這種無狀態(tài)行為使得模型很難將更改通知視圖。在 Web 上,為了發(fā)現(xiàn)對應用程序狀態(tài)的修改,瀏覽器必須重新查詢服務器。 另一個重大變化是實現(xiàn)視圖所用的技術與實現(xiàn)模型或控制器的技術不同。當然,我們可以使用 Java(或者 PERL、C/C++ 或別的語言)代碼生成 HTML。這種方法有幾個缺點: Java 程序員應該開發(fā)服務,而不是 HTML。 更改布局時需要更改代碼。 服務的用戶應該能夠創(chuàng)建網頁來滿足它們的特定需要。 網頁設計人員不能直接參與網頁開發(fā)。 嵌在代碼中的 HTML 很難看。 對于 Web,需要修改標準
57、的 MVC 形式。圖1顯示了 MVC 的 Web 改寫版,通常也稱為 MVC Model 2 或 MVC 2。 圖1. MVC Model 2 Struts,MVC 2 的一種實現(xiàn) Struts 是一組相互協(xié)作的類、servlet 和 JSP 標記,它們組成一個可重用的 MVC 2 設計。這個定義表示 Struts 是一個框架,而不是一個庫,但 Struts 也包含了豐富的標記庫和獨立于該框架工作的實用程序類。圖2顯示了 Struts 的一個概覽。 圖2. Struts 概覽 Struts 概覽 l Client browser(客戶瀏覽器) 來自客戶瀏覽器的每個 HTT
58、P 請求創(chuàng)建一個事件。Web 容器將用一個 HTTP 響應作出響應。 l Controller(控制器) 控制器接收來自瀏覽器的請求,并決定將這個請求發(fā)往何處。就 Struts 而言,控制器是以 servlet 實現(xiàn)的一個命令設計模式。 struts-config.xml 文件配置控制器。 l 業(yè)務邏輯 業(yè)務邏輯更新模型的狀態(tài),并幫助控制應用程序的流程。就 Struts 而言,這是通過作為實際業(yè)務邏輯“瘦”包裝的 Action 類完成的。 l Model(模型)的狀態(tài) 模型表示應用程序的狀態(tài)。業(yè)務對象更新應用程序的狀態(tài)。ActionForm bean 在會話級或請求級表示模型的狀態(tài)
59、,而不是在持久級。JSP 文件使用 JSP 標記讀取來自 ActionForm bean 的信息。 l View(視圖) 視圖就是一個 JSP 文件。其中沒有流程邏輯,沒有業(yè)務邏輯,也沒有模型信息 -- 只有標記。標記是使 Struts 有別于其他框架(如 Velocity)的因素之一。 詳細分析 Struts 圖3示的是 org.apache.struts.action 包的一個最簡 UML 圖。圖3顯示了 ActionServlet (Controller)、 ActionForm (Form State) 和 Action (Model Wrapper) 之間的最簡關系。
60、圖3. Command (ActionServlet) 與 Model (Action & ActionForm) 之間的關系的 UML 圖 ActionServlet 類 您還記得函數(shù)映射的日子嗎?在那時,您會將某些輸入事件映射到一個函數(shù)指針上。如果您對此比較熟悉,您會將配置信息放入一個文件,并在運行時加載這個文件。函數(shù)指針數(shù)組曾經是用 C 語言進行結構化編程的很好方法。 現(xiàn)在好多了,我們有了 Java 技術、XML、J2EE,等等。Struts 的控制器是將事件(事件通常是 HTTP post)映射到類的一個 servlet。正如您所料 -- 控制器使用配置文件以使您不必對這些值進
61、行硬編碼。時代變了,但方法依舊。 ActionServlet 是該 MVC 實現(xiàn)的 Command 部分,它是這一框架的核心。 ActionServlet (Command) 創(chuàng)建并使用 Action 、 ActionForm 和 ActionForward 。如前所述, struts-config.xml 文件配置該 Command。在創(chuàng)建 Web 項目時,您將擴展 Action 和 ActionForm 來解決特定的問題。文件 struts-config.xml 指示 ActionServlet 如何使用這些擴展的類。這種方法有幾個優(yōu)點: 應用程序的整個邏輯流程都存儲在一個分層的文
62、本文件中。這使得人們更容易查看和理解它,尤其是對于大型應用程序而言。 網頁設計人員不必費力地閱讀 Java 代碼來理解應用程序的流程。 Java 開發(fā)人員也不必在更改流程以后重新編譯代碼。 可以通過擴展 ActionServlet 來添加 Command 功能。 ActionForm 類 ActionForm 維護 Web 應用程序的會話狀態(tài)。 ActionForm 是一個抽象類,必須為每個輸入表單模型創(chuàng)建該類的子類。當我說 輸入表單模型 時,是指 ActionForm 表示的是由 HTML 表單設置或更新的一般意義上的數(shù)據(jù)。例如,您可能有一個由 HTML 表單設置的
63、UserActionForm 。Struts 框架將執(zhí)行以下操作: 檢查 UserActionForm 是否存在;如果不存在,它將創(chuàng)建該類的一個實例。 Struts 將使用 HttpServletRequest 中相應的域設置 UserActionForm 的狀態(tài)。沒有太多討厭的 request.getParameter() 調用。例如,Struts 框架將從請求流中提取 fname ,并調用 UserActionForm.setFname() 。 Struts 框架在將 UserActionForm 傳遞給業(yè)務包裝 UserAction 之前將更新它的狀態(tài)。 在將它傳
64、遞給 Action 類之前,Struts 還會對 UserActionForm 調用 validation() 方法進行表單狀態(tài)驗證。 注: 這并不總是明智之舉。別的網頁或業(yè)務可能使用 UserActionForm ,在這些地方,驗證可能有所不同。在 UserAction 類中進行狀態(tài)驗證可能更好。 可在會話級維護 UserActionForm 。 注: struts-config.xml 文件控制 HTML 表單請求與 ActionForm 之間的映射關系。 可將多個請求映射到 UserActionForm 。 UserActionForm 可跨多頁進行映射,以執(zhí)行
65、諸如向導之類的操作。 Action 類 Action 類是業(yè)務邏輯的一個包裝。 Action 類的用途是將 HttpServletRequest 轉換為業(yè)務邏輯。要使用 Action ,請創(chuàng)建它的子類并覆蓋 process() 方法。 ActionServlet (Command) 使用 perform() 方法將參數(shù)化的類傳遞給 ActionForm 。仍然沒有太多討厭的 request.getParameter() 調用。當事件進展到這一步時,輸入表單數(shù)據(jù)(或 HTML 表單數(shù)據(jù))已被從請求流中提取出來并轉移到 ActionForm 類中。 注:擴展 Action 類時請注
66、意簡潔。 Action 類應該控制應用程序的流程,而不應該控制應用程序的邏輯。通過將業(yè)務邏輯放在單獨的包或 EJB 中,我們就可以提供更大的靈活性和可重用性。 考慮 Action 類的另一種方式是 Adapter 設計模式。 Action 的用途是“將類的接口轉換為客戶機所需的另一個接口。Adapter 使類能夠協(xié)同工作,如果沒有 Adapter,則這些類會因為不兼容的接口而無法協(xié)同工作?!保ㄕ?Gof 所著的 Design Patterns - Elements of Reusable OO Software )。本例中的客戶機是 ActionServlet ,它對我們的具體業(yè)務類接口一無所知。因此,Struts 提供了它能夠理解的一個業(yè)務接口,即 Action 。通過擴展 Action ,我們使得我們的業(yè)務接口與 Struts 業(yè)務接口保持兼容。(一個有趣的發(fā)現(xiàn)是, Action 是類而不是接口)。 Action 開始為一個接口,后來卻變成了一個類。真是金無足赤。) Erro
- 溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
5. 裝配圖網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 產后保健知識講座
- 《音樂小屋》課件2
- 2019屆高考物理二輪復習專題二能量與動量第7講動能定理的應用課件
- 灝忓涓€騫寸駭鍥介槻鏁欒偛璇句歡
- 高中地理一輪二輪三輪復習氣象災害集備1
- 人教英語必修二同課異構課件:Unit2TheOlympicGamesSectionAWarmingUpandReading2
- 人教版小學語文二年級上冊《黃山奇石》PPT課件
- 6分數(shù)混合運算(二)第1-課時課件
- 黃河的主人(教育精品)
- 術前肺功能測定及其臨床意義
- 變態(tài)心理學和健康心理學知識專題知識宣講
- 肝纖維化無創(chuàng)性診斷--課件
- 512垂線(1)(教育精品)
- 熒光幻彩金蔥粉耐溶劑金蔥粉
- 第4章音頻媒體2