SpringBoot学习笔记(五):SpringBoot中的静态资源的映射规则

在进行Web开发的时候,会存在大量的静态资源,如:css、image以及jquery等一些文件,那么SpringBoot对静态资源是如何映射的呢?接下来就简单介绍一下SpringBoot中的静态资源的映射规则,去揭开它的神秘面纱。

我们通过init创建SpringBoot项目,可以选择web模块,而SpringBoot默认为我们提供了静态资源的处理,即配置类WebMvcAutoConfiguration,该类存放了与web开发相关的各种配置属性和方法

1、addResourceHandlers()方法

addResourceHandlers()方法对静态资源路径作出了默认的处理,其源码如下:

		@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
			if (!this.resourceProperties.isAddMappings()) {
				logger.debug("Default resource handling disabled");
				return;
			}
			Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
			CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
			//所有的/webjars/** ,都去 classpath:/META-INF/resources/webjars/寻找资源;
			//webjars:以jar包的方式引入静态资源,例如:jquery
			if (!registry.hasMappingForPattern("/webjars/**")) {
				customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}
			//   "/**" 访问当前项目的任何资源,都在静态资源的文件夹找映射
			String staticPathPattern = this.mvcProperties.getStaticPathPattern();
			//静态资源文件夹映射
			if (!registry.hasMappingForPattern(staticPathPattern)) {
				customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
						.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}
		}

2、​ webjars提供的资源文件

以前使用SSM做web项目的时候,利用webapp的目录存放静态资源。但是我们通过。。。创建的SpringBoot项目中并没有这个目录,SpringBoot提供了自己的映射规则。在上面的代码中可以看到webjars以jar包的方式引入静态资源,而且都会去classpath:/META-INF/resources/webjars/中找资源。此时,有人就疑惑了?什么是webjars?通俗的讲:WebJars是以Jar形式为Web项目提供资源文件,然后借助Maven这些依赖库的管理,保证这些Web资源版本唯一性。

以引入jquery为例:

(1)进入webjars的官网:https://www.webjars.org/

(2)进行如下操作。

(3)在springboot项目中引入Maven依赖。

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.4.1</version>
</dependency>

(4)启动springboot项目,访问http://localhost:8080/webjars/jquery/3.4.1/jquery.js。结果如下:

扫描二维码关注公众号,回复: 12846833 查看本文章

 3、一些自定义静态文件的访问

/** 访问当前项目的任何资源。对于自己的静态资源文件,如:JS、CSS、jQuery文件等,SpringBoot默认是从以下这些路径中读取的

"classpath:/META‐INF/resources/"

"classpath:/resources/"

"classpath:/static/"

"classpath:/public/"

"/":当前项目的根路径

例如:访问resource下的bootstrap.js,如下:

启动springboot,访问:http://localhost:8080/bootstrap.js

4、/**映射index.html

在SpringBoot默认静态资源访问路径下添加名为index.html文件,当访问http://localhost:8080/,会自动跳转到这个index.html。也就是说SpringBoot默认首页面为index.html,被" /** "映射。如下:

(1)在public下创建index.html。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>hello SpringBoot</h2>
</body>
</html>

(2)访问:http://localhost:8080/

5、自定义图标 **/ favicon.ico

SpringBoot中可以把ico格式的图标放在默认静态资源文件路径下,并以favicon.ico命名,应用图标会自动变成指定的图标。所有的 **/favicon.ico 都会在静态资源文件下找。步骤如下:

(1)将自己的logo图片转为.ico格式的,命名必须为favicon.ico

(2)将该图片直接放在src/main/resourecs目录下

(3)重启项目。

6、在application.properties中手动配置静态资源访问路径

在application.properties配置文件中如下编辑:

# 自定义静态资源访问路径,可以指定多个,之间用逗号隔开
spring.resources.static-locations=classpath:/dir01/,classpath:/dir02

注意:自定义静态资源访问路径,可以指定多个,之间用逗号隔开,但是当自定义静态资源后,SpringBoot默认的静态资源路径将不再起作用。
 

猜你喜欢

转载自blog.csdn.net/weixin_47382783/article/details/115041247