回顾

之前我们的ssm项目,springmvc的配置文件里面会配置一个静态资源的放行,我们之前会在 webapp文件夹下创建一个static文件夹,里面就写静态资源。之后在springmvc的配置文件里面配置一下静态资源的放行。


以后访问的时候,不需要加这个文件夹static的名字,就可以访问到静态资源。

因为springboot项目也把springmvc集成了,所以配置文件也集成了。人家集成有人家默认的,就告诉你在resources文件夹下,自己创建一个static文件夹,里面写静态资源。项目启动之后,只需要在浏览器输入资源,不需要写static文件夹名字,就可以访问到。

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

2 pom里面 的依赖是

<?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开发的依赖就可以了,以后用到其他的,就再添加

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

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

我们需要看源码,我们找到webmvc的自动配置类
双击shift,搜索webmvcauto


我们分析这个方法

		@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:

if (!this.resourceProperties.isAddMappings()) {
   
				logger.debug("Default resource handling disabled");
				return;
			}

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


里面有一个属性

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

解释2

if (!registry.hasMappingForPattern("/webjars/**")) {
   
				customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
			}

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

已经解决了什么是webjars

解释3

获取静态资源的路径

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