Spring Boot Static where to put resources in the end?

Point of attention, do not get lost; Java-related technologies and continuously updated information! ! !
When we use SpringMVC framework, static resources will be blocked, you need to add additional configuration, before the old and small partners asked a static resource loading problem Spring Boot in the micro letter: "? My HTML page does not seem to Style," Today I by article, and everyone carefully to chat with this problem.

SSM configuration

Boot to talk about problems in the Spring, we have to return to SSM environment to build, in general, we can: do not be configured to intercept a static resource <mvc resources /> node, as follows:

<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/html/**" location="/html/"/>
复制代码

Since this is a matching at Ant-style path, / * indicates the path match any level, so the above abbreviated codes such as the following may also be:

<mvc:resources mapping="/**" location="/"/>
复制代码

This configuration is a configuration in XML, you know, SpringMVC configuration in addition to the configuration in XML, you can also configure the Java code, if configured in the Java code, we only need to customize a class that inherits from WebMvcConfigurationSupport namely can:

@Configuration
@ComponentScan(basePackages = "org.sang.javassm")
public class SpringMVCConfig extends WebMvcConfigurationSupport {
 @Override
 protected void addResourceHandlers(ResourceHandlerRegistry registry) {
 registry.addResourceHandler("/**").addResourceLocations("/");
 }
}
复制代码

Rewrite addResourceHandlers method WebMvcConfigurationSupport class, you can configure a static resource position in the process, consistent with the meaning and implication here above xml configuration, there is no need to say more.

This is our traditional solutions, in Spring Boot, the configuration and the fact that the same strain, but there are some automated configured.

Spring Boot configuration

In Spring Boot, a project if we are created from the https://start.spring.io this site, or use IntelliJ IDEA project in Spring Boot Initialization tool created by default there will be resources / static directory, many small partners also know as long as static resources into this directory, you can directly access, except there's no other static resources can be put place? Why on here will be able to directly access it? This is the problem discussed in this article.

overall plan

First, in the Spring Boot, by default, a total of five positions can put a static resource, five paths are the following five:

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

The first four catalog easy to understand, corresponding to a different directory under resources, 5th / What do you mean it? We know that in Spring Boot project, the default webapp directory is not, of course, we can also add your own (for example, when you need to use the JSP), here for the first 5 / webapp directory is actually a representation of static resources nor interception. If the same file appeared in five directory, then the priority is in the order listed above.

However, while there are five storage directory, except for the first five with less than four other times, the system creates a classpath: / static /, under normal circumstances, we just need to put our static resource directory next to, you do not need additional resources to create other static directory, for example, in my classpath: / static / directory named 1.png a delegation of the picture, then my access path is:

http://localhost:8080/1.png 
复制代码

Here we note that the request does not require static address, if coupled with a static superfluous but will be reported 404 errors. Many people will wonder why it does not need to add static? Obviously placed under static resource directory. In fact, this works well to achieve, for example in the SSM configuration, we configure static resource interception If this is the following:

<mvc:resources mapping="/**" location="/static/"/>
复制代码

If we are so configured, if the requested address is http: // localhost: 8080 / 1.png actually find the relevant files in the system will go /static/1.png directory.

So of course we guess, and perhaps a similar configuration in Spring Boot in.

Source Reading

Mr. Hu Shih said: "bold conjecture, A careful," here we take a look in the end is how to configure the Spring Boot static resources by reading the source code.

First, we see in WebMvcAutoConfiguration class of relevant content SpringMVC automated configuration, find the resources to intercept static configuration, as follows:

See the definition of static resource configuration and the Java SSM here we mentioned earlier is very similar configuration, wherein a value corresponding to the method this.mvcProperties.getStaticPathPattern () is "/ **", this.resourceProperties.getStaticLocations () method returns four positions, namely: "classpath: / META-INF / resources /", "classpath: / resources /", "classpath: / static /", "classpath: / public /", then the method getResourceLocations in, and add a "/", so here the return value of a total of five. Where / represents the webapp directory, static files webapp can be accessed directly. Matching path static resources priority order as defined path. So here is the configuration we mentioned earlier and the same. In this way everyone will know why the Spring Boot support five static resource location, but also understand why the request path does not require a static resource / static, because the path has been mapped automatically added on / static up.

Custom Configuration

Of course, this is the system default configuration, if we do not want to put the resources in the system default five locations, you can also customize static resource location and mapping, self-definition, there are two ways, you can be defined by application.properties , may be defined in Java code, the following were run.

application.properties

Defined is relatively simple in the configuration file as follows:

spring.resources.static-locations=classpath:/
spring.mvc.static-path-pattern=/**
复制代码

The first row represents a configuration defined resource location, the second row represents the configuration rule definition request URL. The text above configuration, for example, if we define a containing the static resources can be placed anywhere in the resources directory, when we visited, of course, also need to write the full path, for example in the resources / static directory is titled 1.png picture, then the access path is http: // localhost: 8080 / static / 1.png, note that at this time of static can not be omitted.

Java code defines

Of course, we may Spring Boot from Java code definitions, Java mode and configured SSM relatively similar, as follows:

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

复制代码

Here the front and codes substantially uniform, relatively simple, not described herein.

to sum up

It should remind you that there are a lot of people after using Thymeleaf, static resources will also be placed under resources / templates directory, note, templates directory is not a static resource directory, it is a place position of page templates (you see Although Thymeleaf template suffix .html, in fact, not static resources). Well, by the above explanation, I believe we are in a position to Spring Boot static resources has a profound understanding, it should not make another mistake in the project!


Reproduced in: https: //juejin.im/post/5d007b785188254ee433c304

Guess you like

Origin blog.csdn.net/weixin_34409741/article/details/93182202