Springboot handles web static resources
Static resources: html, js, css, etc.
Springboot is a jar package, so the static resources are not stored in the webapp (originally Maven project) .
The storage path of static resources is specified by the addResourceHandlers() method in the WebMvcAutoConfiguration class.
In the past, static resources such as js were introduced, and these resources were downloaded and then manually placed in the webapp directory;
Springboot introduces these static resources directly into the project in the form of jar files (maven).
When accessing the imported jar package, start writing from webjars!
Example: Introduce a jQuery, and visit
First download the jar package:
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
Then find the jar package:
Start the project and access the static resource:
http://localhost:8080/webjars/jquery/3.3.1/dist/jquery.js
How to put the static resources written by yourself into springboot?
Method 1: Type the static resources written by yourself into jar packages, and then import springboot.
Method 2: (recommended)
Springboot sets some directory structures into static resource storage directories, and the static resources written by ourselves can be directly placed in these directories!
①"classpath:/META-INF/resources/"
②"classpath:/resources/"
③"classpath:/static/"
④"classpath:/public/"
After storing the resource files in the above directory, you do not need to add a prefix when accessing it, just access it directly!
Example:
First create a new hello.html page in the static resource storage directory:
Then we come to visit him:
When we add the prefix: not accessible
When visiting directly: Success
Set up welcome page
WebMvcAutoConfiguration类中的welcomePageHandlerMapping()->getWelcomePage()->location + "index.html"
That is to say, index.html in any static resource storage directory is the welcome page !
Set the logo
The webpage tags in every website have Logo, for example: the logo of CSDN is
The logo of the webpage tag in the website has a fixed name: favicon.ico
Custom favicon.ico:
By reading the source code, we know: just put favicon.ico into any static resource storage directory!
How to customize static resource storage directory
properties file (prefix+property name)
spring.resources.static-locations=classpath:/res/,classpath:/img/
After customization, the previous default static resource storage path will become invalid!
to sum up:
1. Found through the source code: the storage directory of static resources is
①"classpath:/META-INF/resources/"
②"classpath:/resources/"
③"classpath:/static/"
④"classpath:/public/"
2. Use static resources: just put the static resources in the above directory
3. For other specific files, such as welcome page and logo, you only need to put the convention in the static resource storage directory.