Using SpringBoot for Web development, we need to do things

  1. Importing Static Resources
  2. Home page
  3. JSP, template engine Thymeleaf
  4. Assemble extension SpringMVC
  5. Add and delete
  6. The interceptor

Static resources (three paths)

In SpringBoot, the Web configuration of SpringMVC is in the WebMvcAutoConfiguration configuration class, we directly go to see his source code, analysis of these three paths

At this point, let’s go back to WebPropertier.class (the first image)

Summary: Three paths to loading static resources

  1. throughwebjars: Import requiredwebjarThe dependence of
  2. Load by default: /**
  3. throughclasspath:/META-INF/resources/"."classpath:/resources/"."classpath:/static/"."classpath:/public/, that is, static resources can be placed

  1. through3Load paths have priorities:resourcs>static>public
  2. We can do it atapplication.propertiesthroughspring.mvc.static-path-pattern= /hello/,classpath:/wu/ Custom modifies the default address. After modification, the original autowiring is invalid
  3. classpath:/META-INF/resources/webjars/ Map tolocalhost:8080/webjars/
  4. "classpath:/resources/"."classpath:/static/"."classpath:/public/."classpath:/**"Map tolocalhost:8080/

Home page

Again, we still look at the source of WebMvcAutoConfiguration, continue to see

If you create a new index.html file in the static folder, the contents of the home page will be changed

index.html

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>I am a home page</body>
</html>
Copy the code

A template engineThymeleaf

Why use a template engine?

The HTML page that the front end passes to us is usually turned into a JSP page for easy data transfer. But the SpringBoot project is first and foremost a JAR, not a WAR; Second, we are using embedded Tomcat, so it does not support JSP by default. This is where the template engine comes in.

The premise of Thymeleaf

Introduce the Thymeleaf dependency

<! --thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Copy the code

Thymeleaf source code analysis

We can see the default prefixes and suffixes here!

Conclusion:

  1. All we need to do is put our HTML pages into templates in the classpath and Thymeleaf will automatically render them for us.
  2. With Thymeleaf, you don’t need to configure anything, just put it in the specified folder!

The use of the Thymeleaf

  1. templatesThe test page insidetest.html
<! DOCTYPEhtml>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${msg}"></div>
</body>
</html>
Copy the code

2. The test classThymeleafController.java

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ThymeleafController {
    @RequestMapping("/t1")
    public String test(Model model){
        model.addAttribute("msg"."hello,Thymeleaf");
        return "test"; }}Copy the code
  1. Start the service and run the tests

Assemble extension SpringMVC

SpringBoot does a lot of configuration for SpringMVC. We need to know what configuration is done. How do we customize and extend these configurations

This is where the official documentation comes in: the assembly extension SpringMVC official documentation

Automatic configuration of SpringMVC can be found in the official documentation

We use ContentNegotiatingViewResolver (content negotiation view parser) as an example, the analysis of source:

We find WebMvcAutoConfiguration first, then search ContentNegotiatingViewResolver

We click the ContentNegotiatingViewResolver in continue to see how to locate the view is it

So let’s go ahead and click getCandidateViews and see how do I getCandidateViews

So, ContentNegotiatingViewResolver combines all the view of the parser, we can also according to it, define a view the parser.

Custom view resolver

  1. Write a custom view parser in the main program
package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;

@Configuration// Add the @Configuration annotation to indicate that it is a Configuration class
public class MyMVCConfig implements WebMvcConfigurer {

    //ViewResolver implements all ViewResolver interface classes
    @Bean
    public ViewResolver MyViewResolver(a){
        return new MyViewResolver();
    }

    // Customize the view resolver
    public static class MyViewResolver implements ViewResolver{
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null; }}}Copy the code
  1. toDispatcherServletIn thedoDispatchMethod interrupt point throughdebugTo see if our custom view parser is working

  1. Result: Our view parser was scanned and loaded

conclusion

After analyzing the source code of the custom view parser, we return to the official website

If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations and use it to provide custom instances of those components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.
Copy the code

To change the default Configuration of SpringBoot, write the @Configuration annotation class that inherits WebMvcConfigurer

Why not annotate @enableWebMVC?

The reason is also given on the official website

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.
Copy the code

EnableWebMvc: full takeover, i.e. SpringBoot’s automatic configuration of SpringMVC is invalid, all automatic configuration needs to be configured by ourselves.

I recommend a more detailed explanation of the blog: @enableWebMVC description