SpringBoot 中静态资源的存放位置
先总结,后废话
一定要导入这个依赖,不然访问不了静态资源
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
总结静态资源存放路径有如下几个
classpath为内路径,有两个

1、classpath:/META-INF/resources/webjars/ (这个是jar包中的静态资源,如jquery.jar包中就有 jquery.js)
2、其它我们自己写的静态资源存放路径 我建议就用项目自带 static 文件夹吧,以免出别的bug,
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/",
"/":当前项目的根路径

3、静态资源存放路径下的index.html为首页(欢迎页)
4、 静态资源存放路径下的 favicon.ico 则是图标

在配置文件配置
spring.mvc.favicon.enabled=true
一般默认为true 也就可以不用写了
当然这些内路径我们也可以自己在问价 application.properties 中配置
例如:我定义两个文件夹 hello和world
spring.resources.static-locations=classpath:/hello/,classpath:/world
接下来我们只需在resources根路径下创建hello和world这两个文件夹就可以了。注意,一旦我们自己配置了后路径后,原来默认的几个就无法访问了。两者二选一
在web应用下有webapp 文件夹存放静态资源和html等文件,而在SpringBoot 中是没有这个文件夹的,而且它的存放路径也有规定的(被类WebMvcAutoConfiguration 所限定 IDEA快捷搜索类 Ctrl+Shift+Alt+N)
2、SpringBoot对静态资源的映射规则;
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
这段代码所规定:
1)、所有/webjars/**,都去classpath:/META-INF/resources/webjars/找资源;
webjars:以jar包的方式引入静态资源;网址是 http://www.webjars.org/ 可以从中得到Maven坐标。
以jquery为例
<!-- 引入jquery -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
导入后可以看到

也可以测试一下是否可以访问,没问题的话是可以的 localhost:8080/webjars/jquery/3.3.1/jquery.js
2)、访问当前项目的任何资源,(静态资源的文件夹)
if (!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/",
"/":当前项目的根路径
例如:localhost:8080/abc===去静态资源文件夹里面找abc
3)、欢迎页;
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(this.getInterceptors());
return welcomePageHandlerMapping;
}
静态资源文件夹下的所有index.html页面;被”/**”映射;localhost:8080/找index页面
4)、图标
@Configuration
@ConditionalOnProperty(
value = {"spring.mvc.favicon.enabled"},
matchIfMissing = true
)
public static class FaviconConfiguration implements ResourceLoaderAware {
private final ResourceProperties resourceProperties;
private ResourceLoader resourceLoader;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(-2147483647);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler.setLocations(this.resolveFaviconLocations());
return requestHandler;
}
private List<Resource> resolveFaviconLocations() {
String[] staticLocations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
List<Resource> locations = new ArrayList(staticLocations.length + 1);
Stream var10000 = Arrays.stream(staticLocations);
ResourceLoader var10001 = this.resourceLoader;
var10001.getClass();
var10000.map(var10001::getResource).forEach(locations::add);
locations.add(new ClassPathResource("/"));
return Collections.unmodifiableList(locations);
}
}
所有的**/favicon.ico都是在静态资源文件下找; favicon.ico 图标名
网友评论