Introduction to creating MVC applications using Struts

Source: Internet
Author: User
Tags tld

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.

Model 1

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:

  1. The implementation of an application is generally based on a process. A group of JSP pages implement a business process. To modify the process, you must modify it in multiple places. This is not conducive to application expansion and updates.
  2. Because the application is not built on a module, the business logic and presentation logic are not abstracted and separated in the JSP page. Therefore, it is not conducive to Application System Business reuse and modification.

Considering these problems, different design patterns must be used for developing large Web applications -- this is Model2.

Model 2

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 ).

  1. You must rethink and design the application structure based on MVC components. Applications that can be implemented by creating a simple JSP page are now designed and implemented in multiple steps.
  2. All pages and components must be implemented in the MVC Framework, so additional development is required.

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.

  1. Controller: the Controller accepts requests from the client, executes the corresponding business logic, and sends the response result back to the client. In Struts, the Controller function consists of the ActionServlet and ActionMapping objects in the figure. The core is a Servlet-type object ActionServlet, which is used to accept client requests. ActionServlet includes a set of configuration-based ActionMapping objects. Each ActionMapping object maps a request to an Action processor object in a specific Model.
  2. Model: The Model part in the MVC system can be divided into two types: internal state of the system and action to change the state of the system. Struts provides Action and ActionForm objects for the Model: All Action processor objects are subclasses derived from the Action class of Struts. 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. It can describe the client form data by defining attributes. Developers can derive subclass objects from them and use them in combination with the custom tag library provided by Struts to encapsulate and support the form data on the client, an Action processor object can be read and written directly without data interaction with the request and response objects. The ActionForm Component Object supports interaction between views and models. Struts generally recommends that you use a set of JavaBean to indicate the internal status of the system. You can also use components such as Entity EJB and Session EJB to implement the system status based on the complexity of the system. Struts recommends separating "what to do" from "how to do. In this way, business logic can be reused.
  3. View: The View part of the Struts application is implemented through JSP technology. Struts provides a custom tag library for use. These custom tags can interact well with the system's Model part. JSP forms created by using these custom tags can be used, you can map to the ActionForm in the Model to encapsulate user data. These custom tags also provide various display functions such as template customization.

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 ):

  1. Collect and define application requirements.
  2. Defines and develops "screen display" requirements based on the principles of data collection and display.
  3. Define the access path for each "screen display.
  4. Define the relationship between ActionMappings and application business logic.
  5. Develop all support objects that meet the "screen display" requirement.
  6. Create an ActionForm object based on the data attributes provided by each "screen display" requirement
  7. Develop the Action object called by ActionMapping.
  8. Develop application business logic objects (beans, ejbs, and so on ).
  9. Create a JSP page for the ActionMapping design process.
  10. Create an appropriate profile struts-config.xml, web. xml.
  11. Development/test/deployment

When using the Struts framework, the development of each part mainly includes:

  1. Model: design and implement the business logic of the system using the JavaBean and EJB components. The specific Action processing object is derived from the Action according to different requests. Complete the "what to do" task to call the service components composed of beans. Create a derived class of ActionForm to encapsulate the form data of the client.
  2. Controller part: Struts provides us with the implementation of the core control part. We only need to configure the ActionMapping object
  3. View part: to use the ActionForm object in the Model, we must use the custom tag provided by Struts to create an HTML form. Use the custom tag library provided by Struts to compile the user interface to separate the application logic from the display logic. The Struts framework uses these custom tags to establish the relationship between views and models. The custom tag of Struts also provides many custom page functions.
  4. You also need to edit two configuration files: web. xml and struts-config.xml. Configure the interaction between modules in the Struts system. The following describes the two configuration files:

    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:

    <servlet><servlet-name>action</servlet-name><servlet-class>org.apache.struts.action.ActionServlet</servlet-class><init-param><param-name>application</param-name>・・・・・・</servlet>

    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.

    <servlet-mapping><servlet-name>action</servlet-name><url-pattern>*.do</url-pattern></servelt-mapping>

    Description: ing between the url Information of the customer request and the specific processing on the server side.

    <taglib><taglib-url>/WEB-INF/struts-bean.tld</taglib-url><taglib-location>/WEB-INF/struts-bean.tld</taglib-location></taglib>・・・・・・・

    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

    <form-beans><form-bean name="loginForm" type="loginForm" /></form-beans>

    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.

    <action-mappings><actionpath="/login"type="loginAction"name="loginForm"input="/login.jsp" ・・・ /></action-mappings>

    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.

References

  1. The Struts User's Guide http://Jakarta.apache.org
  2. "Struts Tutorial" by: Stephen Wiesner homepage www.stephen wiesner.de
  3. Struts series Article http://www.onjava.com/onjava/open_source/
  4. Design Patterns, Gamma, and others
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: zhaochenxi@vip.sina.com
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.