This article describes how to use Struts to create a MVC application.
Introduction to Model 1 and Model 2
A concept we often mention when developing a Web application is Model 1/Model 2. What does it mean? In fact, It is a description of the different models that use JSP technology to form Web applications. Next we will give a brief introduction to this concept.
In the use of JAVA technology to build Web applications, due to the development of JSP technology, this easy-to-Master and fast-developed technology has become the main technology for creating Web applications. JSP pages can be easily displayed on jsp pages in combination with business logic (jsp: useBean), server processing process (JSP: scriplet), and HTML (
Of course, this development mode has great advantages in fast and small-scale application development, but it also has some disadvantages from the engineering perspective:
Considering these problems, different design patterns must be used for developing large Web applications -- this is Model2.
Model 2 represents a framework based on the MVC pattern. MVC is short for Model-View-Controller. "Model" indicates the business logic of the application (implemented by the JavaBean and EJB components), and "View" indicates the application surface (generated by the JSP page ), "Controller" provides application processing process control (generally a Servlet). This design model divides the application logic, processing process and display logic into different components for implementation. These components can be used for interaction and reuse. This makes up for the shortcomings of Model 1.
Model 2 has the advantages of componentization, making it easier to develop and manage large-scale systems. However, developing MVC systems is much more complicated than simply developing JSP systems, it requires more time to learn and master. At the same time, the introduction of new things will bring new problems (this reminds me of an article about "Automatic Computing", which mentions that in order to reduce the complexity of the system, it leads to a higher complexity ).
MVC itself is a very complex system, so when using MVC to implement Web applications, it is best to select a ready-made MVC framework for development, so as to get twice the result with half the effort. There are a lot of available MVC frameworks. Because Struts has complete documentation and is relatively simple, it is easier to use it to develop the MVC system.
Struts structure and processing process
Struts is a project organized by Apache. Like other projects organized by Apache, it is also an open source project. Struts is a good MVC framework that provides underlying support for developing MVC systems. The main technologies used are Servlet, JSP, and custom tag library. For the version and details of its use, see the http://jakarta.apache.org website.
The basic structure of the Struts framework is as follows:
Figure 1 struts uml diagram
As a MVC Framework, Struts provides implementation components for Model, View, and Controller, corresponding to the above UML diagram, and see how they are combined.
The processing process of the Struts framework clearly reflects the characteristics of the MVC system, as shown in simple Struts component structure 2. Struts Controller ActionServlet processes customer requests and maps requests to the Action processor object using the configured ActionMapping object for processing. Action processes the data of an object accessing ActionForm, processes and responds to customer requests. It also calls the Bean components in the background, which encapsulate the specific business logic. The Action processor notifies the Controller of the processing result and the Controller proceeds with the next step.
Figure 2 component structure of the Struts Framework
What we need to do to develop the MVC system using the Struts Framework
Because Struts has provided us with a very good MVC framework, we can greatly speed up development when developing the MVC system using Struts. A development process that can be used during development is as follows (Reference 3 ):
When using the Struts framework, the development of each part mainly includes:
Configuration of the web. xml file:
Web. xml in web applications is the first place to be configured. It describes the Controller object of the system. Add the following tag to web. xml:
Note: this servlet object is the Controller provided by Struts. You can also specify initialization parameters for it, such as support for system application attributes.
Description: ing between the url Information of the customer request and the specific processing on the server side.
Note: Add a reference to the custom tag library used by the application provided by Struts.
Struts-config.xml file Configuration:
Struts-config.xml is used to establish the relationship between the Controller and the Model. It describes the request corresponding to the specific processing rule used by the Controller, and also describes the ing between the data provided by the customer and the ActionForm component.
Add the following tag in the struts-config.xml
Note: The <form-bean> tag describes a specific ActionForm subclass object. It can be used with custom tags on the JSP page to realize data ing between ActionForm and View.
Note: The <action-mappings> MARK describes the one-to-one ing between requests and processes. The input and path attributes uniquely indicate a request from the client. The name attribute describes the ActionForm subclass object that encapsulates the data of the client. The Type attribute describes the Action subclass object that processes the request.
Through the configuration of the two configuration files, the MVC part of the Struts framework is linked to implement a real MVC system.
Examples for Reference
Compile a good example to demonstrate how much space is occupied. For detailed installation steps, refer to the attached documentation. By referring to these examples, we can quickly understand how to use the struts framework to develop the MVC system.
|About the author
Zhao Chenxi, who has been focusing on server-side program development, prefers to use C ++ and Java programming and has a great interest in new technologies. He is currently personally interested in J2EE and Open Source projects. Hope to communicate with more fans. Email: firstname.lastname@example.org