Struts2 overview-MVC Mode

Source: Internet
Author: User

1.1 Introduction to MVC pattern 1.1.1 MVC pattern

MVC is an architectural model. It does not introduce new functions, but is used to guide us to improve the application architecture, so that the application model and view are separated, to achieve better development and maintenance efficiency.

In the MVC mode, an application is divided into three parts: Model, View, and Controller. The model part contains the application's business logic and business data; the view Part encapsulates the application's output form, that is, the page or interface commonly referred; the Controller is responsible for coordinating the model and view, selecting the model to be called based on user requests to process the business, and the view to which the user responds.

The responsibilities of the three parts of the MVC model are very clear and separate from each other. Therefore, each part can be changed independently without affecting other parts, which greatly improves the flexibility and reusability of the application.

1.1.2 MVC model Basics

In the MVC mode, an application is divided into three parts: Model, View, and Controller. Next, let's take a look at these three parts.

1: first, let's take a look at the functions of models, views, and controllers.

  • Model: encapsulates the state of an application and implements its functions. Data Models are usually divided into data models and business logic models. data models are used to store business data, such as order information and user information. business logic models include business operations of applications, for example, add or modify an order.
  • View: displays the model content to users. Users can request the model to be updated through the View. The view obtains the data to be presented from the model and then presents the data to the user in its own way, which is equivalent to providing an interface for human-computer interaction with the user. After the user completes operations on the interface or fills in the information, click the submit button or send a request to the Controller in other trigger events.
  • Controller: used to control the application process and process the requests sent by the view. After the Controller receives a user request, It maps the user data to the model update, that is, it calls the model to implement the user request function; then, the Controller selects the view for the response and displays the updated model data to the user.

    2: Relationship between models and views

    In MVC, the model and view are separated. Generally, there is no logical implementation in the view, and the model does not depend on the view. The same model may have many different display methods, that is, the same model can correspond to multiple different views.

    Such examples are everywhere. For example, when you browse folders on a windows operating system, the data in folders remains unchanged, but there are many ways to display them, for example, large icons, small icons, detailed information, and so on. Take the objects in the tomcat7 folder as an example and follow different display methods, as shown in Figure 1.1:

    Figure 1.1 A model corresponds to multiple views

    The model is responsible for the output content, while the view is responsible for the output form. The model does not depend on the view, and the model and view are decoupled. Therefore, when you modify a view, that is, the display mode, you do not have to worry about the model. Instead, you only need to directly modify the display mode of the view.

    3: MVC component Diagram

    MVC component relationship 1.2 is shown in:

    Figure 1.2 MVC component Diagram

    The component relationship diagram of MVC describes the interaction between the model, view, and controller. The interaction relationships are described in detail in the interaction sequence below:

    (1) first, the view is displayed to the user. The user operates on the view and enters some business data.

    (2) then the user will click the submit button to send the request

    (3) The view sends a user request to the controller. The request contains the business functions and related data to be completed.

    (4) The Controller processes user requests, encapsulates the data in the requests, selects and calls an appropriate model, and requests the model to update the status, then select the view to be displayed to the user.

    (5) The model processes the business functions of user requests and maintains and updates the model status.

    (6) When the model status changes, the model notifies the corresponding view to tell the view that its status has changed.

    (7) after receiving a notification from the model, the View queries the status of the model, obtains the data to be displayed, and then displays the data according to the display method of the view.

    The next step is to wait for the user to perform the next operation.

    1.1.3 role of MVC Mode

    In early development, some programmers did not realize the benefits brought by the MVC pattern and did not follow the MVC pattern during development. The result is that the program structure is not clearly divided, and the functions of each part are chaotic. When the business functions are changed, many classes need to be modified, whether it is the business logic modification or the display form modification, as a result, the efficiency of software development and maintenance is low, with hundreds of errors.

    Following the MVC pattern to develop the system will greatly avoid the above problems. The core method of the MVC model is decoupling. The MVC model divides the entire application into three parts, namely model, view, and controller, then, the communication between the three parts is strictly controlled to obtain an application with clear structure, reasonable function distribution, reusable, scalable, and maintainable.

    Therefore, the following benefits can be obtained by using the MVC mode:

    • Low coupling: In the MVC mode, the model and view are decoupled, and the model does not depend on the view. The view only obtains the data to be displayed from the model, it is not associated with the logic processing of the model.
    • Lower development costs: the MVC model helps us clearly divide the responsibilities of each part, so that programmers can perform their respective duties. Java programmers only care about the implementation of business logic, that is, the model part; the interface programmer only cares about page display, that is, the view part.
    • Better maintainability: the MVC model divides clear models and views and decouples them. When software requirements change, they can independently change without affecting each other, this makes the program easier to maintain and expand. 1.1.4 MVC in Model2 Model

      In Java Web development, the Servlet + JSP + JavaBean model is usually called the Model2 model, which fully complies with the MVC model and is divided as follows:

      • As a model, JavaBean can be used as a data model to encapsulate business data and as a business logic model to include business operations of applications. The data model is used to store or transmit business data. After receiving the model update request from the Controller, the business logic model executes the specific business logic processing and returns the corresponding execution result.
      • As the presentation layer, JSP is responsible for providing pages to present data to users and providing corresponding forms for user requests, in addition, when appropriate (for example, the user clicks the submit button), the controller sends a user request to request the model to be updated.
      • Serlvet, as a controller, is used to receive user-submitted requests, obtain the data in the request, convert it to the data model required by the business model, and then call the corresponding business methods of the business model, update the request model, and select the view to return based on the business execution results, that is, select the next page.

        Shows the basic structure chart of Model2 to implement MVC:

        Figure 1.3 MVC in Model2

        What is the basic response sequence of the Servlet + JSP + JavaBean model after a user sends a request?

        When a user sends a request, the request is received by the Controller Servlet. The Servlet converts the request data to the data model JavaBean, and then calls the business logic model JavaBean method, put the results returned by the business logic model into appropriate places, such as the request attributes. Finally, the Controller selects the appropriate view (JSP) based on the results returned by the business logic model ), data is presented to users by views.

        Private school online website original "grinding struts2" Series

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.