Application of spring MVC module in springboot project


Background analysis
When the project becomes bigger and bigger, the business in the project will become more and more complex. If we only use one object to deal with it
For all businesses, the complexity of this object will be higher and difficult to maintain, and the solution to similar problems in life and actual projects will be better
Solutions tend to adopt the idea of “divide and rule” to reduce business complexity and improve its maintainability,
According to what rules to divide, it needs to have a certain design, so MVC design idea was born

What is MVC?
1) Layered design idea
2) A routine or design pattern
3) It is the abbreviation of model, view and controller
3.1) model (business logic object)
3.2) controller (control logic object)
3.3) view

MVC design of web module in spring framework
The web module in spring is implemented based on MVC design idea

1) Dispatcher servlet (core controller in spring MVC controller)
two ) RequestMapping ( Encapsulated request mapping -> Mapping from URL to specific handler and method )
3) Handler (request processor, which is responsible for processing controller acquisition request, which can be understood as model)
4) Modelandview (object encapsulating business data and view information)
5) Viewresolver (object responsible for template view resolution – > View)

Implementation of Web MVC QuickStart in springboot project
1) Create project module
2) Add project dependencies (spring web, thymeleaf)
3) Project structure analysis (static, templates)
4) Project startup analysis (start running startup class, check whether the service is started or not)
5) Modify the project port and restart the analysis
6) Create HTML pages in static directory and templates directory respectively, and then conduct access test
6.1) create directly in the static directory index.html After starting the service, directly access the browser to test, test OK
six . two ) Create a default in the templates directory . html , Direct browser access test after starting the service , 404 exception
6.3) define templatecontroller and related methods in the project, and return the string default inside the method(
This string can be understood as the name of the view), and then the service is started based on the request URL mapping defined on the method
(@ requestmapping (“/ dotemplate UI”)) access method to detect test output.

What files (HTML template files) does the templates Directory Store
Can the HTML page of the templates directory be accessed directly in the browser? may not
What resources can I put in the static directory ?( html / css / js / images )
Is there any difference between the HTML files stored in the templates directory and the static directory?
HTML in static directory can be accessed directly in browser, but it can’t be parsed by view parser in spring
The HTML page of templates directory can’t be accessed directly in browser, but can be parsed by spring’s view parser,
So as to give HTML more capabilities (such as thymeleaf expressions, branch statements, loop statements,…)
What is thymeleaf? (is a template engine using HTML as a template, which adds HTML elements
Additional attributes are parsed and then enabled. Thymeleaf is supported by default in springboot project
Design and configuration )

1) 404 exception (web request resource does not exist)
2)TemplateInputException (template may not exists)

5. Advanced configuration of Web MVC application in springboot project

1) Pre suffix configuration of thymeleaf template engine in springboot project
2) Automatic page refresh configuration in springboot project (after modifying the page content, you can see the updated content without restarting the service)

1) What is modelandview? (spring MVC module provides an object encapsulating response data by default)
2) What scenario is modelandview used in? (response data includes both view and model)
3) How do you understand JSON
four ) How to use POJO in springboot project by default , Map and other similar objects into JSON format string ?( With Jackson )
5) What is the function of @ reponsebody annotation? (describes the method in handler, which is used to tell the bottom layer how to modify the method
For example, for the method described by the annotation, its return value is definitely not view, and it tells the underlying method the conversion of the return value
The results are finally written to the response body and then transmitted to the client.)
6) What is the function of the @ restcontroller annotation? (describes the handler type, which is used to define that this class is a controller
Request processing object, and all methods in the class add @ ResponseBody annotation by default)

Recommended Today

Analysis of super comprehensive MySQL statement locking (Part 1)

A series of articles: Analysis of super comprehensive MySQL statement locking (Part 1) Analysis of super comprehensive MySQL statement locking (Part 2) Analysis of super comprehensive MySQL statement locking (Part 2) Preparation in advance Build a system to store heroes of the Three KingdomsheroTable: CREATE TABLE hero ( number INT, name VARCHAR(100), country varchar(100), PRIMARY […]