SpringBoot对静态资源的映射规则--webjars和静态资源文件夹映射

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wu2374633583/article/details/83963760

首先配置规则的映射源码是如下这个类:

 WebMvcAutoConfiguration 

		@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的映射
			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));
			}
		}

如果想在配置文件里面配置静态资源的参数的话:

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {

1 webjars的映射

webjars:以jar包的方式引入静态资源;

常用静态资源文件的webjars: https://www.webjars.org/

映射规则:
所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;

<dependency>
		    <groupId>org.webjars</groupId>
		    <artifactId>jquery</artifactId>
		    <version>3.3.1-1</version>
		</dependency>

在这里插入图片描述
访问路径:
http://127.0.0.1:8080/webjars/jquery/3.3.1-1/jquery.js

2 静态资源文件夹的映射规则

有时候我们需要去映射自定义的静态资源,那么我们就需要知道springboot它是怎么来映射我们的资源文件和我们的文件应该怎么放。

规则如下:
“/**” 访问当前项目的任何资源,都去(静态资源的文件夹)找映射:

"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径

注意这里所说的classpath指的是:src/main/resources
比如:
在这里插入图片描述
访问路径为:
http://127.0.0.1:8080/asserts/css/bootstrap.min.css

3 欢迎页的配置

源码:

@Bean
		public WelcomePageHandlerMapping welcomePageHandlerMapping(
				ApplicationContext applicationContext) {
			return new WelcomePageHandlerMapping(
					new TemplateAvailabilityProviders(applicationContext),
					applicationContext, getWelcomePage(),
					this.mvcProperties.getStaticPathPattern());
		}
private Optional<Resource> getWelcomePage() {
			String[] locations = getResourceLocations(
					this.resourceProperties.getStaticLocations());
			return Arrays.stream(locations).map(this::getIndexHtml)
					.filter(this::isReadable).findFirst();
		}
	private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
			"classpath:/META-INF/resources/", "classpath:/resources/",
			"classpath:/static/", "classpath:/public/" };

可以根据源码看到静态资源文件夹下的所有index.html页面;被"/**"映射;

localhost:8080/ 找index页面

4 小图标的映射

源码如下:

			@Bean
			public SimpleUrlHandlerMapping faviconHandlerMapping() {
				SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
				mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
				mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
						faviconRequestHandler()));
				return mapping;
			}

所有的 **/favicon.ico 都是在静态资源文件下找;

猜你喜欢

转载自blog.csdn.net/wu2374633583/article/details/83963760