【正文】
nge. MVC decouples interface from business logic and data. Struts is an MVC implementation that uses Servlets and JSP 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. ModelViewController (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 es to the 大學畢業(yè)設計(論文) 第 2 頁 rescue. MVC helps resolve some of the issues with the single module approach by dividing the problem into three categories: ? Model The model contains the core of the application39。s 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. 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 sets 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 requery the server 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 that 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 isn39。 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. 大學畢業(yè)設計(論文) 第 4 頁 ? 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。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 大學畢業(yè)設計(論文) 第 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 Con