Background analysis When more and more projects do big, project business will become more and more complicated, if we only use an object to handle all of the business, the complexity of this object will be higher and difficult to maintain, life and solutions to the problems of similar schemes in practical projects tend to the ideas of the “divide and conquer”. To reduce business complexity and improve its maintainability. Now the problem is how to divide, according to what rules to divide, this needs to have a certain design, so the MVC design idea was born.

What is MVC? 1) Layered Design Idea 2) A Routine or Design Pattern 3) Abstraction of Model,View,Controller

The Web module in Spring is implemented based on the MVC design idea, and its core components are as follows:

1)DispatcherServlet (core Controller in Spring MVC) 2)RequestMapping (encapsulated RequestMapping – mapping between > URLs to specific handlers and methods) 3)Handler(request Handler -> is responsible for handling Controller fetch request -> can be thought of as model) 4)ModelAndView (the object that wraps business data and View information) 5)ViewResolver (the object responsible for template View parsing ->View)

1) Create a project module 2) Add a project dependency (Spring Web, Thymeleaf) 3) Static and Templates 4) Project Startup Analysis (Start and Run Startup Clases4) 6) Create an HTML page in the static and templates directories and run the test. 6.1) Create an index.html page in the static directory and run the test directly in the browser after starting the service. 6.2) Create Default. HTML in the Templates directory, access the test directly from the browser after starting the service,404 exception 6.3) Define the TemplateController and its associated methods in the project, Returns the string Default inside the method (you can think of this as the name of the view), and then starts the service to access the method based on the request URL mapping defined on the method (@RequestMapping(“/doTemplateUI”)) to detect the test output.

Templates directory what files to store (HTML template files) Can the HTML pages of the templates directory be accessed directly from the browser? Static: What resources can you put in a static directory? What’s the difference between the templates directory and the static directory for HTML files? So the HTML in the static directory is accessible in the browser but it’s not parsed by the view parser in Spring and the HTML pages in the templates directory are not accessible in the browser but it’s parsed by the view parser in Spring, To add more capabilities to HTML (such as Thymeleaf expressions, branch statements, loops, etc.) What is Thymeleaf? HTML is a template engine that takes HTML as a template, adds additional attributes to HTML elements, parses them, and then enforces them. Thymeleaf is supported and configured by default in SpringBoot projects.

Bug? 2)TemplateInputException (template may not exist)

5. Advanced configuration of Web MVC application in SpringBoot project

1)SpringBoot project Thymeleaf template engine prefix and suffix configuration 2)SpringBoot project page automatic refresh configuration (after modifying the page content, do not restart the service can see the updated content)

1) What is ModelAndView? (By default, the Spring MVC module provides an object that encapsulates the response data.)

2) In what scenario is ModelAndView generally applied? The response data contains both a View and a Model.

3) How do you understand JSON? (It’s just a lightweight data format that supports cross-end requirements)

4) How to convert POJOs, maps and similar objects into JSON strings by default? (with the help of a Jackson)

5) What does the @ReponseBody annotation do? Describes a method in a Handler that tells the underlying layer what to do with the method

The return value is processed, such as the method described in this annotation, whose return value is definitely not a View, and the underlying method is told to convert the return value

The result is eventually written to the response body and then transmitted to the client.

6) What does the @RestController annotation do? Describes the Handler type used to define this class as a Controller

Request processing object, and all methods in the class are annotated by @responseBody by default.