一、静态资源访问
1.1 静态的内容
Spring Boot serves static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext.
1 静态资源目录
- 使用根路径+静态资源名即可访问
- static
- public
- resources/resources
- META-INF/resources
以上为默认静态资源访问路径
2 原理
- 静态资源映射:
/**
- 请求先进来,先去找Controller能不能处理,如果不能就去静态资源处理器处理,如果还找不到就是404。
3 访问前缀配置
我们可以为静态资源配置访问前缀,以免我们后期进行权限控制时候影响。/
static-path-pattern
spring:
mvc:
static-path-pattern: /res/** #对于欢迎页、小图标影响
4 WebJar(了解即可)
- 自动映射/webjars/**
- 资源网址
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
- 配置完成后通过http:localhost:8080/jquery/3.5.1/jquery.js
1.2 欢迎页支持
- 再静态资源下放入index文件即可
- 可以使用静态资源路径,但不要使用前缀功能,否则是失效。
spring:
# mvc:
# static-path-pattern: /res/** 这个会导致welcome page功能失效
resources:
static-locations: [classpath:/haha/]
1.3 自定义 Favicon
- favicon.ico 放在静态资源目录下即可。