SpringBoot-static resource access

1. Directly access the template page

 In a  Spring Boot  project, if a template engine is used, there may be some template pages that do not need to load data through the controller, but only need to jump directly to display. When using SpringMVC  in the past  , if you visit a page, you must write the corresponding  Controller  class. To achieve this requirement Spring Boot  only needs to directly implement the  WebMvcConfigurer interface and override the  addViewControllers  method to configure the mapping relationship, without writing the corresponding  Controller  class.

1. Add a login.html page in the resources/templates directory

 2. Define the MVC configuration class to implement the  WebMvcConfigurer interface and rewrite its  addViewControllers  method.

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
    }
}

3. Restart the login page of the method .

 

Two, static resource access

In Spring MVC , developers need to manually configure static resource filtering for static resources. SpringBoot also provides automatic configuration for this, which can simplify the configuration of static resource filtering.

1. The default configuration strategy

(1) Spring Boot has performed the default static resource filtering configuration here, where staticPathPattern is defined by default in WebMvcProperties , and the definition is as follows:

private String staticPathPattern = "/**";

(2) The obtained default static resource location is defined in  ResourceProperties  , the code is as follows:

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
				"classpath:/resources/", "classpath:/static/", "classpath:/public/" };

Note that according to the defined order, the priority of the 5 static resource locations decreases sequentially

If a developer uses IntelliJ IDEA to create a Spring Boot project, the classpath:/static/ directory will be created by default , and static resources can generally be placed in this directory. As follows:

 (3), restart the project, we visit, http://localhost:8080/001.jpg 

2. Custom strategy

If the default static resource filtering strategy cannot meet the development needs, you can also customize the static resource filtering strategy. There are two ways to customize it.

2.1, defined in the configuration file

(1). The filtering rules and static resource location can be directly defined in application.properties , the code is as follows:

//过滤规则
spring.mvc.static-path-pattern=/static/**

//静态资源位置
spring.web.resources.static-locations=classpath:/static/

There is a static internal class WebMvcAutoConfigurationAdapter in the WebMvcAutoConfiguration class , which implements the WebMvcConfigurer interface.

There is a method addResourceHandlers in the WebMvcConfigurer interface  , which is used to configure static resource filtering.

The method is implemented in the WebMvcAutoConfigurationAdapter class.

(2), restart to visit  http://localhost:8080/static/001.jpg 

2.2, Java coding definition

(1) Implement the WebMvcConfigurer interface, and then implement the addResourceHandlers method of the interface , the code is as follows:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**") //过滤策略
                .addResourceLocations("classpath:/static/");  // 静态资源路径
    }

}

 (2) The effect of restarting the project is the same as the above.

Guess you like

Origin blog.csdn.net/small_love/article/details/111715791