Two: MVC architecture pattern (very important!!!)

table of Contents

One: Introduction to the MVC architecture pattern

MVC architecture pattern:

Two: Instructions for implementing MVC in Java,

1.Model (model): ***Service class:

2. Controller (controller): Servlet class:

3. View (view): .jsp or .ftl file:

4. MVC can make View display and Model data decoupling manifestation analysis

Three: the advantages of MVC architecture pattern 


One: Introduction to the MVC architecture pattern

          ● MVC architecture pattern: It is a very important architecture pattern in the entire software field.

          ● When preparing to design and develop a piece of software, the first step is not to write code, but to design the way the software is built, and first determine the development model;

          ● Common in software engineering are: MVC architecture pattern; MVVM architecture pattern; the main purpose of these architecture patterns is to make software better develop, so that teams can better collaborate in teams, so that modules and modules have lower Coupling.

MVC architecture pattern:

●  View : JSP is only used to display data, which is a view;

●  Model : The back-end Java class is dedicated to data processing and calculation, and the result is the model;

●  Controller :

            (1) There is no direct connection between the view and the model, otherwise it will become as in the "display and code coupling" example above (the front-end code and the back-end code are mixed);

            (2) In order to prevent a direct connection between the view and the data, a new role is added: the controller;

            (3) The controller is the most important link in the MVC architecture pattern; it serves as a link between the previous and the next; it is responsible for receiving request parameters from the view, passing the parameters to the Model for data processing, and then returning the results of the Model data processing to the view through the controller ; The view and the model are directly facing the controller, the view does not need to know the existence of the back-end model, and the model does not need to know which view is using my data;

            (4) The controller, which effectively decouples the view and the model;

In the MVC mode, it is easy to divide the work of the team; the front-end personnel develop the view and the back-end personnel develop the model. After the development is completed, the front and back ends are glued together through the controller;

MVC is not a specific pronoun. MVC is a design concept. MVC can be applied to Java or .NET. The implementation of MVC in different languages ​​is different;


Two: Instructions for implementing MVC in Java,

1.Model (model): ***Service class:

……………………………………………………

2. Controller (controller): Servlet class:

……………………………………………………

3. View (view): .jsp or .ftl file:

MVC can decouple View display and Model data;

……………………………………………………

4. MVC can make View display and Model data decoupling manifestation analysis

MVC can decouple View display and Model data; where does the decoupling manifest:

It is conceivable that the engineer who develops JSP (belonging to View) is a junior engineer. During development, he does not need to care about which class the back-end data is generated. He only needs to make an agreement with the senior engineer who develops the Model. This result is ours. Finally, it will be stored in the "squareList" attribute of the current request; after that, the JSP engineer will directly extract the data from the result, and where the data comes from, the junior engineer who develops the JSP does not need to know. ; Only need to traverse and output "squareList" based on the predetermined attribute name;

When the MVC structure pattern is introduced, the maintainability of the project is greatly improved; it can be imagined that (1) When the manager said that the current project no longer uses JSP as the template engine, all need to be replaced with FreeMarker, at this time the underlying business logic There is no need to rewrite at all, we only need to change the View view technology in JSP to FreeMarker template; (2) One day, the engineer who developed the Model found that a certain functional logic may be better and more efficient. To implement the writing method, at this time, after the engineer modifies the code of the Model, the front-end interface is completely unaware; because the engineer who develops the Model only makes adjustments in the method, as long as the previously agreed attribute name remains unchanged, there is no problem at all.

It can be found that the functions of developing JSP and the functions of developing background business logic are separated, and the two do not interfere with each other; on this basis, the division of labor and cooperation of the team will be very coordinated as a whole;

MVC not only guarantees the decoupling at the software level, but also guarantees the organizational structure of teamwork;


Three: the advantages of MVC architecture pattern 


Note:

      ● View (view) part, need to extract background data, and html combined together, this work requires a template engine, JSP is a template engine, FreeMarker is also a template engine! ! !

      ● Naturally, the template engine of JSP is the standard of JEE. Tomcat has built-in JSP implementation in the manufacturer’s implementation and can be used directly; the file is .jsp;

      ● The template engine FreeMarker is not a JEE standard. If you want to use FreeMarker, you need to import the jar package and configure it; the file is .ftl;

      ● Although there are differences in the usage strategy and specific grammar between JSP and FreeMarker, the overall usage routines of the two are very similar; the core of JSP is el and jstl expressions, and the core of FreeMarker is freemarker grammar and built-in functions;

 

Guess you like

Origin blog.csdn.net/csucsgoat/article/details/114683477