【正文】
s 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. ? 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。 ActionForm) The ActionServlet class 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. 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 hardcode the values. Life changes, but 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 file configures the Command. During the creation of the Web project, Action and ActionForm are extended to solve the specific problem space. The file 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 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 repile code when making flow changes. Command functionality can be added by extending ActionServlet. The ActionForm class ActionForm maintains the session state for the Web application. ActionForm is an abstract class that is subclassed 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 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。設(shè)計(jì)漂亮的網(wǎng)站需要做大量的工作,大多數(shù) Java 開(kāi)發(fā)人員更注重創(chuàng)建優(yōu)美的對(duì)象接口,而不是用戶(hù)界面。我們還得處理驗(yàn)證、流程控制和更新應(yīng)用程序的狀態(tài)等問(wèn)題。視圖可以訪(fǎng)問(wèn)模型的讀方法,但不能訪(fǎng)問(wèn)寫(xiě)方法。 另一個(gè)重大變化是實(shí)現(xiàn)視圖所用的技術(shù)與實(shí)現(xiàn)模型或控制器的技術(shù)不同。圖 4 顯示了 MVC 的 Web 改寫(xiě)版,通常也稱(chēng)為 MVC Model 2 或 MVC 2。 文件配置控制器。其中沒(méi)有流程邏輯,沒(méi)有業(yè)務(wù)邏輯,也沒(méi)有模型 信息 只有標(biāo)記。 現(xiàn)在好多了,我們有了 Java 技術(shù)、 XML、 J2EE,等等。文件 指示 ActionServlet 如何使用這些擴(kuò)展的類(lèi)。當(dāng)我說(shuō) 輸入表單模型 時(shí) ,是指 ActionForm 表示的是由 HTML 表單設(shè)置或更新的一般意義上的數(shù)據(jù)。 注: 這并不總是明智之舉。 Action 類(lèi)的用途是將 HttpServletRequest 轉(zhuǎn)換為業(yè)務(wù)邏輯。 考慮 Action 類(lèi)的另一種方式是 Adapter 設(shè)計(jì)模式。 Action 開(kāi)始為一個(gè)接口,后來(lái)卻變成了一個(gè)類(lèi)。 確定 Controller 調(diào)用哪個(gè) Action 類(lèi)。 ? 標(biāo)記庫(kù) 為什么要另發(fā)明 一種輪子,或標(biāo)記庫(kù)呢?如果您在庫(kù)中找不到您所要的標(biāo)記,那就自己定義吧。 Struts 的缺點(diǎn) ? 仍處于發(fā)展初期 Struts 開(kāi)發(fā)仍處于初級(jí)階段。在開(kāi)發(fā)網(wǎng)頁(yè)的過(guò)程中,我們是否應(yīng)該讓網(wǎng)頁(yè)設(shè)計(jì)人員訪(fǎng)問(wèn) Java 代碼?某些框架(如 Velocity)說(shuō)不應(yīng)該,但它提供了另一種 Web 開(kāi)發(fā)語(yǔ)言讓我們學(xué)習(xí)。無(wú)論您決定使用哪種類(lèi)型的框架,您都應(yīng)該了 解您要在其中部署和維護(hù)該框架的環(huán)境。毫無(wú)疑問(wèn),要理解 Struts 必須接受一定的培訓(xùn)。