The basic concept of SpringMVC is about three-tier architecture and MVC three-tier architecture. Our development architecture is generally based on two forms, one is C/S architecture, which is client/server, and the other is B/S architecture, which is browser server. In JavaEE development, almost all development is based on B/S architecture. Then in B/S architecture, the system standard three-layer architecture includes: presentation layer, business layer, and persistence layer. Three-tier architecture is used a lot in our actual development, so many cases are based on three-tier architecture design.
In a three-tier architecture, each layer has a role to play. Let’s talk about what each layer is responsible for:
The presentation layer:
This is what we call the Web layer. It is responsible for receiving client requests and responding to the results to the client. Usually, the client uses HTTP protocol to request the Web layer. The Web needs to receive HTTP requests and complete HTTP responses. The presentation layer includes the presentation layer and the control layer: the control layer is responsible for receiving requests, and the presentation layer is responsible for displaying results. The presentation layer relies on the business layer. Upon receiving the client request, the business layer is generally called for business processing and responds to the client with the processing result. Presentation layer design typically uses the MVC model. (MVC is the design model of the presentation layer, and has no relationship with other layers)Copy the code
The business layer:
Also known as the Service layer. It handles business logic and is closely related to the requirements of our development project. The Web layer depends on the business layer, but the business layer does not depend on the Web layer. The business layer may rely on the persistence layer for business processing, and transaction consistency is required if data is to be persisted. (As we said, transactions should be controlled in the business layer)Copy the code
The persistence layer:
Also known as the DAO layer. Responsible for data persistence, including data layer, namely database and data access layer, database is the carrier of data persistence, data access layer is the interface between business layer and persistence layer, the business layer needs to persist data into the database through the data access layer. Generally speaking, the persistence layer is the interaction with the database, the database table has been deleted.Copy the code
The full name of MVC is Model View Controller, which is the abbreviation of Model – View – Controller. It is a pattern used to design and create the presentation layer of Web applications. Each part of MVC does its job: Model: Usually refers to our data Model. Generally, it is used to encapsulate data. View: This usually refers to our JSP or HTML. It’s usually about presenting data. Views are usually created from model data.
Controller: Is the part of the application that handles user interaction. The effect is generally processor logic. For example, we want to save a user’s information, which includes name, gender, age, and so on. The age must be an integer between 1 and 100. The name and gender cannot be blank. And populate the model with data. At this time in addition to the JS check, the server should also have data accuracy check, so the check is the controller should do. When the verification fails, the controller displays the error page to the user. If the validation is successful, the controller is responsible for populating the model with data and calling the business layer to fulfill the complete business requirements.
SpringMVC is a lightweight Java-based request-driven Web FrameWork that implements the MVC design model. SpringMVC is a successor to the Spring FrameWork. Has been integrated into Spring Web Flow. The Spring framework provides a full-featured MVC module for building Web applications. Use Spring’s pluggable MVC architecture, so that when using Spring for WEB development, you can choose to use Spring’s Spring MVC framework or integrate with other MVC development frameworks, such as Struts1(currently not used), Struts2, etc. SpringMVC has become one of the most mainstream MVC frameworks, and with the release of Spring3.0, it has comprehensively surpassed Struts2 and become the best MVC framework. It uses a set of annotations to make a simple Java class a controller for handling requests without implementing any interfaces. It also supports RESTful programming style requests.
The position of SpringMVC in the three-tier architecture
Advantages of SpringMVC 1. Clear role division: Front-end Controller (DispatcherServlet) request to processor (HandlerMapping) processor adapter (HandlerAdapter) ViewResolver (ViewResolver) processor or page Controller (Controller) validator ( Validator) Command Object (the Object to which Command request parameters are bound is called Command Object) Form Object (the Object to which Form objects are provided for Form presentation and submission is called Form Object). 2, the division of labor is clear, and the extension point is quite flexible, can be easily expanded, although rarely needed. 3. Since a command object is a POJO, it can be used directly as a business object without inheriting framework-specific apis. 4. Seamless integration with other Spring frameworks that no other Web framework has. 5, adaptive, through the HandlerAdapter can support any class as a processor. 6, customizable, HandlerMapping, ViewResolver can be very simple customization. 7, powerful data verification, formatting, binding mechanism. Spring provides Mock objects that make it easy to unit test the Web layer. 9. Support for localization and theme analysis makes it easier to switch between internationalization and theme. 10, powerful JSP tag library, make JSP writing easier.