Static Resource Import

  • Read the document if you don’t understand it, (Chinese version 2.1.5)

  • By default, Spring Boot will service static content from a directory named /static (/public, /resources, or/meta-INF /resources) in the classpath or ServletContext root. It USES the Spring MVC ResourceHttpRequestHandler, so you can add your own WebMvcConfigurerAdapter rewrite addResourceHandlers method to modify this behavior.

  • By default, resources are mapped to /, but can be adjusted with the spring.mvc. Static-path-pattern property. For example, to relocate all resources to /resources/ :
spring.mvc.static-path-pattern=/resources/**
Copy the code
  • You can also customize the locations of static resources using the spring.resources.static-locations attribute (replace the default with a list of directory locations). Root the Servlet context path/Automatically added as a location.

Home Page (Welcome page)

  • Spring Boot supports both static and template-based welcome pages. It starts by looking for the index.html file in the configured static content location (static, /public, /resources). If not, look for the Index template. If any of them are found, it is automatically used as the welcome page for the application.

A template engine

  • You can use Spring MVC to serve dynamic HTML content. Spring MVC supports a variety of template technologies, including Thymeleaf, FreeMarker, and JSP. Of course, many other template engines also have their own Spring MVC integration.

Extensions for SpringMVC

  • If you want to keep the functionality of Spring Boot MVC and need to add additional MVC configurations (interceptor, Formatter, view controller, etc.), You can add your own @Configuration class of type WebMvcConfigurerAdapter, but not with the @enableWebMVC annotation. If you want to custom RequestMappingHandlerMapping, RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver instance, You can declare a WebMvcRegistrationsAdapter instance to provide these components.

  • If you want full control of Spring MVC, you can add a custom @Configuration class annotated with @EnableWebMVC.

  • The sample

@Configuration
public class Myconfig implements WebMvcConfigurer {
    // the ViewResolver implements the class of the ViewResolver interface, so we can treat it as a ViewResolver
    @Bean
    public ViewResolver myViewResolver(a) {
        return new MyViewResolver();
    }

    // create a custom view resolver MyViewResolver
    public static class MyViewResolver implements ViewResolver {
        @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception {
            return null; }}}Copy the code

A custom starter@Configuration+@Conditionalxxx+@xxxProperties