前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >springboot系列学习(十一):springboot项目里面的静态资源的详解,不怕找不到了

springboot系列学习(十一):springboot项目里面的静态资源的详解,不怕找不到了

作者头像
一写代码就开心
发布2020-11-20 10:10:09
发布2020-11-20 10:10:09
89600
代码可运行
举报
文章被收录于专栏:java和pythonjava和python
运行总次数:0
代码可运行

1 创建一个springboot项目,用脚手架创建的

2 pom里面 的依赖是

代码语言:javascript
代码运行次数:0
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.hibernate.validator</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>6.0.17.Final</version>
			<scope>compile</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

其实只要在这个里面导入web开发的依赖就可以了,以后用到其他的,就再添加

代码语言:javascript
代码运行次数:0
运行
复制
	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

3 这个静态资源可以放到项目的什么位置

我们需要看源码,我们找到webmvc的自动配置类

双击shift,搜索webmvcauto

我们分析这个方法

代码语言:javascript
代码运行次数:0
运行
复制
		@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();
			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));
			}
		}

解释1:

代码语言:javascript
代码运行次数:0
运行
复制
if (!this.resourceProperties.isAddMappings()) {
				logger.debug("Default resource handling disabled");
				return;
			}

如果这个资源已经自定义,那么直接返回。那么我们如何自定义呢?

里面有一个属性

根据以上的类里面的东西在yml里面我们自定义一下就可以了。这样就自定义了静态资源的路径

解释2

代码语言:javascript
代码运行次数:0
运行
复制
if (!registry.hasMappingForPattern("/webjars/**")) {
				customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}

找项目里面的webjars目录,但是我们在我们的项目里面没有看到。现在我们要解决的是什么是webjars?

已经解决了什么是webjars

解释3

获取静态资源的路径

代码语言:javascript
代码运行次数:0
运行
复制
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
			if (!registry.hasMappingForPattern(staticPathPattern)) {
				customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
						.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}

根据这段代码,进入里面,找到5个位置放静态资源

根据源码放静态资源的路径,我们在我们的项目里面,找到对应的地方,创建源码里面的文件夹

意思是我们在自己创建的3个文件夹下面自己定义一个静态文件,项目启动的话,就可以找到

项目启动。访问

因为源码里面已经配置了文件夹的名字,所以我们在访问的时候,就不需要写文件夹的名字了

如果3个文件夹下面的资源的名字是一样的,那么先加载哪一个了?

优先级是:

resourses > static > public

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/09/29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 创建一个springboot项目,用脚手架创建的
  • 2 pom里面 的依赖是
  • 3 这个静态资源可以放到项目的什么位置
  • 解释1:
  • 解释2
  • 解释3
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档