A statement by the author in this paper: jiangnan rain authorized reprint The original link: www.imooc.com/article/293…

preface

When we use SpringMVC framework, static resources will be blocked and additional configuration needs to be added. Before, a friend asked Songgo on wechat about static resource loading in Spring Boot: “Songgo, my HTML page seems to have no style?” Today, I will talk about this problem with you in detail through an article.

1. Configure the SSM

To talk about problems in Spring Boot, we need to go back to the SSM environment setup. In general, we can configure the < MVC :resources /> node to not intercept static resources, as follows:

<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/html/**" location="/html/"/>

Copy the code

Since this is an Ant style path card, /** means that any level of path can be matched, the above code could also be abbreviated as follows:

<mvc:resources mapping="/ * *" location="/"/>

Copy the code

So this configuration is in XML, and you know SpringMVC can be configured in Java code as well as in XML, and if you configure it in Java code, you just have to customize a class, Inherited from WebMvcConfigurationSupport can:

@Configuration
@ComponentScan(basePackages = "org.javaboy.javassm")
public class SpringMVCConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/ * *").addResourceLocations("/"); }}Copy the code

Rewrite WebMvcConfigurationSupport addResourceHandlers method in class, configure static resource location in this method, the meaning and the meaning of the XML configuration above, there is no need to say more.

This is our traditional solution. In Spring Boot, the configuration is similar to this, but with some automation.

2. Configure Spring Boot

In Spring Boot, if we are creating a project from the website https://start.spring.io, or using the Spring Boot initializer in IntelliJ IDEA, By default, there is a directory called resources/static. Many people know that static resources can be accessed directly by putting them in this directory. Why can I access it directly here? That’s what this article is about.

2.1 Overall Planning

First, by default, there are five locations for static resources in Spring Boot. The five paths are as follows:

  1. classpath:/META-INF/resources/
  2. classpath:/resources/
  3. classpath:/static/
  4. classpath:/public/
  5. /

The first four directories correspond to different directories in the resources directory. What does the fifth/mean? We know that the webApp directory is not included in the Spring Boot project by default, but we can add it ourselves (for example, when we need to use JSPS), and the fifth/means that static resources in the WebApp directory are not intercepted. If the same file appears in five directories, the priority is in the order listed above.

By default, classpath:/static/ is created. Normally, we only need to put our static resources in this directory, and we don’t need to create other static resource directories. For example, if I have an image named 1.png in the classpath:/static/ directory, my access path would be:

http://localhost:8080/1.png

Copy the code

If static is not required, 404 will be generated. Many people may wonder why you don’t add static. The resource is clearly in the static directory. In fact, this effect is easy to achieve, for example, in the SSM configuration, our static resource interception configuration is as follows:

<mvc:resources mapping="/ * *" location="/static/"/>

Copy the code

If we are such a configuration, request the address if is http://localhost:8080/1.png system will actually go/static / 1. PNG directory to find the relevant documents.

So it’s only natural to assume that Spring Boot might have a similar configuration.

2.2 Source Code Interpretation

Mr. Hu Said: “bold guess, careful verification”, we here through the source code interpretation to see how Spring Boot static resources in the end is configured.

First of all, we looked at the automatic configuration of SpringMVC in the WebMvcAutoConfiguration class. We found the configuration of static resource interception as follows:

The definition and we can see here the static resources mentioned Java configuration of SSM configuration is very similar, among them, the enclosing mvcProperties. GetStaticPathPattern () method, the corresponding value is / * *, This. The resourceProperties. GetStaticLocations () method returns the position of the four, respectively is:

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/

Then in the getResourceLocations method, you add /, so there are five returned values. / indicates the WebApp directory, that is, static files in webApp can also be accessed directly. The matching paths of static resources have lower priorities. So the configuration is exactly the same as we mentioned earlier. You can see why five static resource locations are supported in Spring Boot, and why /static is not required in the static resource request path, because /static is automatically added to the path map.

2.3 Custom Configuration

This is the default configuration. If you don’t want to put resources in the default five locations, you can also customize static resource locations and mappings. You can also customize static resource locations and mappings in two ways: application.properties or Java code.

2.3.1 application. The properties

The method defined in the configuration file is simple as follows:

spring.resources.static-locations=classpath:/
spring.mvc.static-path-pattern=/**

Copy the code

The first configuration defines the resource location and the second configuration defines the request URL rule. For example, if we define static resources in this way, we can put static resources anywhere in the resources directory. We also need to write the full path when we access static resources. For example, if we have an image named 1.png in the resources/static directory, we can put static resources anywhere in the resources directory. Then the access path is http://localhost:8080/static/1.png, pay attention to at this time of the static cannot be omitted.

2.3.2 Java Code Definition

Spring Boot can also be customized using Java code, similar to the Java configuration of the SSM, as follows:

@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/ * *").addResourceLocations("classpath:/aaa/"); }}Copy the code

Here the basic code and the previous consistent, relatively simple, no longer repeat.

3. Summary

One thing to note here is that When Songo sees a lot of people using Thymeleaf, he puts static resources in the Resources /templates directory as well. Notice that the templates directory is not a static resource directory. It is a place to put page templates (the Thymeleaf template you see is not a static resource, even though the suffix is.html). Well, through the above explanation, I believe you have a deep understanding of the location of Spring Boot static resources, should not be in the project error bar!

Seek a wave of support

Welcome to pay attention to the technology public number [terminal RESEARCH and development Department], chatterbox technology, workplace, recruitment, online interview, advanced promotion. Nothing is impossible, only the unexpected. Reply 1024 to get the relevant learning materials