Eight, SpringBoot Web development access to static resources (with + detailed source code analysis)
@
- Eight, SpringBoot Web development access to static resources (with + detailed source code analysis)
- 1. Basic introduction
-
2. Quick start
- 2.1 Preparations
- 3. Change the static resource access prefixes to what we want them to be
- 4. Change the default static resource path in Spring Boot (to achieve custom static resource path)
- 5. Static resource access considerations and details
- 6. Summary:
- 7. Finally:
1. Basic introduction
Access to static resources in SpringBoot:
- Static resources can be accessed directly by placing them under the class paths: /static, /public, /resources, /META-INF/resources - the corresponding files (This is set by default in Spring Boot ). On this point, we start with the This class can be found on the source code, with the corresponding configuration properties.
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", " classpath:/static/", "classpath:/public/"};
Note: classpath:/resources/ means that the server will look for it under the resources path, and you can't enter the resources directory when you enter the url address in the browser, because the server will look for it under classpath:/resources/, and if you write resources/ in the browser, you want to express the name of the url address. in the browser, what you want to express is: let the browser from the path of resources/resouces to find, which can not be found to report a 404 error!
Note: classpath:/resources/ means that the server will look in the resources path, you can not enter the url address in the browser, you can not enter the resources directory, because the server is in the classpath:/resources/ to find, and if you write resources/, you want to express: let the browser from the resources/resouces path to find, this is not found to report a 404 error! in the browser, what you want to express is: let the browser from the path of resources/resouces to find, which can not be found to report a 404 error!
- Common static resources: js, css, images (.jpg, .png, .gif, .bmp, .svg), font files (Fonts) and so on.
- Access method: Default: project root path/+static resource name e.g..http://localhost:8080/ The answer to this question can be found in the class. The answer to this can be found in the class.
2. Quick start
2.1 Preparations
Import the relevant jar dependencies in the pom .xml file. As follows
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="http:///2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId>springboot_static_configuration</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- import (data)SpringBoot parent project-style of writing (grammar)-->
<parent>
<groupId></groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
</parent>
<!-- import (data)webProject Scene Launcher:会自动import (data)和webdevelopment-relatedjarPackage all dependencies【storehouse/jar】-->
<!-- I'll explain later.spring-boot-starter-web What exactly are the relevant dependencies introduced-->
<dependencies>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Write the startup program:
package ;
import ;
import ;
@SpringBootApplication // Logo activation scenarios
public class Application {
public static void main(String[] args) {
(,args);
}
}
topBasic Introduction Among other things, we learned that Spring Boot's default access paths for static resources are4 We'll test here whether these four paths areDirect access to 。
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
Note: classpath means the class path, that is, as shown below:.resources
The catalog, in a nutshellclasspath === (equivalent to) resources
。
Below: We create the default four Spring Boot directories under the resources class path. As shown in the figure below:
In the meantime we put a few images in these four directories for access testing.
Start the program to run the test:
Open a browser for direct access to static resource files:
Attention:
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", " classpath:/static/", "classpath:/public/"}; Note: classpath:/resources/ means that the server will look for it under the resources path, and you can't enter the resources directory when you enter the url address in the browser, because the server will look for it under classpath:/resources/, and if you write resources/ in the browser, you want to express the name of the url address. in the browser, what you want to express is: let the browser from the path of resources/resouces to find, which can not be found to report a 404 error!
resources/static is Spring Bopt's default static path, the default is to "resources/static" as the root path to access, so there is no need to add additional static in the browser, if you add, then you are actually accessing the If you add static in the browser, then you actually access the path: resources/static/static, this path does not exist in the resources, so the error can not be found.
3. Change the static resource access prefixes to what we want them to be
Change the static resource access prefix, e.g. we want thehttp://locahost:8080/rainbowsea/* Under the request path to request static resources, application scenario: static resource access prefix and controller request path conflict.
We need to use theyaml
If you want to know more about yaml syntax, please go to ✏️✏️✏️.Spring Boot when the yaml syntax to use-CSDN Blogs
First of all, we are in theresources
class path to create a file named of the document.
Prepare the following:
spring:
mvc:
static-path-pattern: /rainbowsea/**
Run the test:
was changed by us to our own
/rainbowsea/**
Note: The latter/**
It cannot be omitted . Otherwise it's inaccessible.
4. Change the default static resource path in Spring Boot (to achieve custom static resource path)
Spring Boot also supports us to customize static resource paths for increased flexibility.
Change the default static resource path, e.g., we add the test directory under the class path as a static resource path ourselves and complete the test.
Again, to change the default static resources in Spring Boot, we'll use the yaml syntax here. In theresources
class path to create a file named of the document.
yaml is written as follows:
spring.
web.
resources: # modify/specify static resource access path/location
# Modify/specify the access path/location of static resources
static-locations: ["classpath:/test/"] # parody
# Note: Try to start on the far left for more hints!
Under the resources class path, create a test directory, and in that directory, place a file named of the picture, visit the test.
Open a browser to access the test:
Essentially: static-locations modifies the value of the staticlocations property in the WebProperties class (i.e., the springboot
the default static path).
So here we have modified the default static resource path of Spring Boot, and the resources previously placed under the default static path of Spring Boot cannot be accessed.
Wanted: To keep the original Spring Boot default static resource paths, just add the original Spring Boot default paths.
Run the test:
5. Static resource access considerations and details
- Note: If you put it directly under the root path of resources class, you can't access it. Because we know from the source code of the WebProperties class that there are only four default static resource paths in Spring Boot, and the path of the resources class does not belong to one of these four.
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
Run a test: we put an image in the resources class path and try to see if it can be accessed directly.
Open a browser to access the test:
- When the default request path of the resource's name and Controller Controller request processing the same path, the conflict. ** Priority to see if the Controller can handle; can not, the processing of the request to the static resources to deal with, if the static resources can not be found also the corresponding point of resources, then report: 404 can not be found, the page.
Static resources are accessed principle: static mapping is /** , that is, all requests are intercepted, the request comes in, first look at the Controller can not handle, can not handle the request to the static resources to deal with, if the static resources can not be found, the corresponding 404 page
package ;
import ;
import ;
@RestController // @Controller + @ResponseBody
public class HiController {
@RequestMapping("") // Controller controls the path of the request to be processed and the name of the static resource to conflict with.
public String hi(){
return "hi"; }
}
}
6. Summary:
- Understand that Spring Boot static resources are placed under the class paths: /static, /public, /resources, /META-INF/resources and can be accessed directly - the corresponding files (This is set by default in Spring Boot ). On this point, we start with the This class can be found on the source code, with the corresponding configuration properties.
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", " classpath:/static/", "classpath:/public/"};
Note: classpath:/resources/ means that the server will look for it under the resources path, and you can't enter the resources directory when you enter the url address in the browser, because the server will look for it under classpath:/resources/, and if you write resources/ in the browser, you want to express the name of the url address. in the browser, what you want to express is: let the browser from the path of resources/resouces to find, which can not be found to report a 404 error!
- Access method: Default: project root path/+static resource name e.g..http://localhost:8080/ The answer to this question can be found in the class. The answer to this can be found in the class.
- Change the static resource access prefix, e.g. we want thehttp://locahost:8080/rainbowsea/* Under the request path to request static resources, application scenario: static resource access prefix and controller request path conflict.
- Change the default static resource paths in Spring Boot (implement custom static resource paths).
- Note: If you put it directly under the root path of resources class, you can't access it. Because we know from the source code of the WebProperties class that there are only four default static resource paths in Spring Boot, and the path of the resources class does not belong to one of these four.
7. Finally:
"In this final chapter, I would like to express my gratitude to each and every one of my readers. Your attention and responses have been a source of motivation for me to create, and I have drawn endless inspiration and courage from you. I will keep your encouragement in my heart and continue to struggle in other areas. Thanks to you all, we will always meet again at some point."