Introduction to the MVC application with Struts

xiaoxiao2021-03-06  192

December 1, 2002. Model 1 and Model 2 Introduction We often mentioned in developing web applications is Model 1 / Model 2, then what does it mean? In fact, it is a description of different models that make up a web application using JSP technology. Here is a simple introduction to this concept. Model 1 In an example of establishing a web application using Java technology, due to the development of JSP technology, this soon that this easy to master and realize rapid development has become the main technique for creating web applications. The JSP page can be very easy to combine business logic (JSP: UseBean), the server processing (JSP: scriplet), and HTML (), and simultaneously implement display, business logic, and process control simultaneously in the JSP page, so that the application can be completed quickly Development. A lot of web applications now is made up of a set of JSP pages. This development model-centric development model we can call Model 1. Of course, this development model has a very advantage when conducting fast and small-scale applications, but from the perspective of engineering, it also has some shortcomings:

The implementation of the application is generally based on the process, a set of JSP pages implements a business process. If you want to make changes, you must modify multiple places. This is very disadvantageous to apply extensions and updates. Since the application is not built on the module, business logic and representation logic mixed in the JSP page did not perform abstract and separation. So very disadvantageous to the reuse and modification of the application system business. Considering these issues must adopt different design patterns when developing large web applications - this is the MODEL2 Model 2 Model 2 represents a MVC mode-based framework. MVC is a short written by Model-View-Controller. "Model" is the application's business logic (implemented by javabean, ejb component), "view" is the application's representation surface (generated by JSP page), "Controller" is a process control (generally a servlet) The application logic, processing procedure, and display logic are achieved by this design model into different components. These components can interact and reuse. This makes up for the shortcomings of Model 1. Model 2 has the advantages of componentization, making it easier to achieve development and management of large-scale systems, but developing MVC systems is more complicated than simple JSP development, it requires more time learning and master. At the same time, the introduction of new things will bring new problems (this reminds me of an article about "Automatic Calculation", which refers to the complexity of the system, resulting in higher complexity).

The application structure must be rethinked and designed based on the MVC component. It turns out that the application that can be implemented by establishing a simple JSP page now has become a designed and implementation process of multiple steps. All pages and components must be implemented in the MVC framework, so additional development must be performed. The MVC itself is a very complex system, so when using the MVC to implement a web application, it is best to choose an off-the-shelf MVC framework, which is developed under this, thereby obtaining a half-time effect. There are now many MVC frameworks available, because Struts have a complete document and relatively simple, so use it develops the MVC system or more convenient. 2. Struts Structure and Processing Process Introduction Struts is a project of Apache organizations, like other Apache organizations, and it is also an open source project. Struts is a better MVC framework for providing the underlying support for the development of the MVC system, which is the main technique of use of servlet, JSP, and Custom Tag Library. Getting its usage versions and specific information can be found at http://jakarta.apache.org website. The basic composition of the Struts framework is shown in the figure below: Figure 1 Struts UML

As a framework of MVC, both Struts provide corresponding components to Model, View, and Controller, corresponding to the UML map above, and see how they are combined.

Controller: The role of the controller is to accept requests from the client and select Perform the corresponding business logic and then send the response result back to the client. In Struts Controller function consists of an ActionServlet and an ActionMapping object in the figure: The core is an object ActionServlet for a servlet type, which is used to accept the client's request. The ActionServlet includes a set of configuration-based ActionMapping objects that implements a request to a specific MODEL section between an Action processor object. The Model section in the MVC system can be divided into two types-system internal states, and the action of changing the system status. Struts provides an Action and ActionForm objects for the Model section: All Action processor objects are subclasses of developers from Struts Action class. The Action processor object encapsulates the specific processing logic, calls the business logic module, and submits the response to the appropriate VIEW component to generate a response. The ActionForm component object provided by Struts, which can describe client form data by defining attributes. Developers can derive sub-objects from it, using the custom marker combination with Struts, which can achieve good packages and support for the client's form data, Action processor objects can read and write it directly, and no longer need Data interaction with the request, response object. Support for interaction between View and Model is implemented via the ActionForm component object. Struts typically recommends using a set of JavaBeans to represent the internal state of the system, and can use components such as Entity EJB and Session EJB according to the complexity of the system. System status. Struts recommends separating "what" and "how" (how to do "(business logic) when implementing. This can achieve the reuse of business logic. View: The View section in the Struts application is implemented through JSP technology. Struts provides custom tag libraries that can be used very well with these custom tags to interact with the MODEL part of the system, and the JSP form created by using these custom tags can implement mappings in the model section, completed Package for user data, while these custom tags also provide a variety of display functions such as template customization. The processing process of the Struts framework refers to the characteristics of the MVC system, and the Simple Struts component structure is shown in Figure 2. The Struts Controller ActionServlet handles the client request, and the request is mapped to the ACTION processor object with the configured actionMapping object. Action Processing Object Accesses Data, Processing, and Response Customer Request in Actionform, which also calls the bacan component of the background that encapsulates the specific business logic. The Action processor object notifies the Controller in accordance with the processing result, and the Controller performs the next process. Figure 2 Structure of the Struts framework

3. Using the Struts framework to develop MVC systems to do due to Struts has provided us with a very good MVC framework, we can greatly accelerate the speed of development when using Struts to develop MVC systems. A development process that can be used during development is as follows (from information 3):

Collect and define applications. Define and develop "Screen Display" requirements based on data acquisition and display. Define access paths for each "Screen Display". Define the connection between ActionMappings to build to the application business logic. Develop all support objects that meet the requirements of the "Screen Display". Based on the data properties provided by each "screen display" requirements to create the corresponding ActionForm object development Action object called by actionMApping. Develop application business logic objects (Bean, EJB, etc.). Create a JSP page corresponding to the ActionMapping design. Establish a suitable configuration file struts-config.xml, web.xml. Development / Test / Deployment When using the Struts framework, the development of each part mainly includes: Model section: Using JavaBean and EJB components, design, and implementing the system's business logic. Detect specific Action processing objects from Action depending on the request. Complete the "What to do" is to call the business component composed of beans. Create a package that implements a derived class of ActionForm to client form data. Controller section: Struts provides us with the implementation of the core control section. We only need to configure an actionMApping object View section: To use the ActionForm object in Model, we must create an HTML form with custom tags provided by Struts. Separate the user interface with the custom tag library provided by Struts to separate the application logic and display logic. The Struts framework establishes the contact between View and Model through these custom tags. Struts's custom tag also provides a lot of custom pages. At the same time, you need to edit two profiles: Web.xml and Struts-Config.xml. Configure the interaction between each module in the Struts system. Here, do some of these two profiles: Web.xml file configuration: web.xml in web applications is where to configure, which describes the system's Controller object. Increase the following tags in Web.xml

Action

Org.apache.struts.Action.ActionServlet

Application

······

Description: This servlet object is the Controller provided by Struts, and you can specify initialization parameters, such as support for system application properties.

Action

* .do

Description: Implement the URL information of the client request and the specific processing of the server-side specific processing.

/Web-inf/struts-bean.tld

/Web-inf/struts-bean.tld

·······

Note: Adding a reference to the custom tag library used by the application provided by Struts. The configuration of the struts-config.xml file: Struts-config.xml is the relationship between Controller and MODEL. It describes the rules that control the request to the specific processing in Controller, and it also describes the data provided by the customer and the corresponding mapping relationship of the ACTIONFORM component. Increase the following tags in Struts-Config.xml

Description:

Marking describes a specific ActionForm subclass object that enables data mapping between ActionForm and View by combining a custom marker in it and the JSP page.

Description:

The tag describes a one-to-one mapping relationship of request and processing. The INPUT and PATH attribute uniquely labeled a request for the client, and the Name property describes the Actionform subclass object for the data of the package client. The Type property describes the Action subclass object that handles this request.

Through the configuration of the two profiles, connect the various parts of the MVC in the Struts framework to realize a real MVC system. 4. Examples of the available reference to prepare a good example showing a comparison, you can download the latest publishes of Struts from http://jakarta.apache.org/, there is a detailed user help and API documentation in this package, There is a very good demo program for reference. Specific installation steps can refer to the included document. Refer to these examples can quickly understand how to develop the MVC system using the Struts framework. References: The Struts User's Guide http://Jakarta.apache.org "Struts Tutorial" Author: Stephan Wiesner Home www.stephanwiesner.de Struts series http://www.onjava.com/onjava/open_source/ "Design Patterns "Gamma and other four" Zhao Chenxi, which has been concentrated on server-side procedures, likes to use C and Java to have great interest in new technologies, current personal interests on J2EE and Open Source on J2EE. Hope and more enthusiasts communicate. Email: zhaochenxi@vip.sina.com

转载请注明原文地址:https://www.9cbs.com/read-128625.html

New Post(0)