15 SpringBoot静态资源的映射规则

1.1 WebMvc自动配置

  • org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
// 添加资源映射
public void addResourceHandlers(ResourceHandlerRegistry registry) {
	if (!this.resourceProperties.isAddMappings()) {
		logger.debug("Default resource handling disabled");
	} else {
		// 从ResourceProperties中获取缓存时间
		Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
		CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
		
		if (!registry.hasMappingForPattern("/webjars/**")) {
			// 详见 1.2 webjars,请求为/webjars/**
			this.customizeResourceHandlerRegistration(registry.
				// /webjars/**的请求
				addResourceHandler(new String[]{"/webjars/**"}).
				// 都在 classpath:/META-INF/resources/webjars/ 中获取资源
				addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).
				// 设置缓存时间
				setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
		}

		// staticPathPattern = "/**";
		String staticPathPattern = this.mvcProperties.getStaticPathPattern();
		
		// 详见 1.3 请求为 /**
		if (!registry.hasMappingForPattern(staticPathPattern)) {
			this.customizeResourceHandlerRegistration(registry.
				// /**请求
				addResourceHandler(new String[]{staticPathPattern}).				
				// 都在"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"中获取资源
				addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).
				
				setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
		}

	}
}
  • ResourceProperties
// 设置和静态资源有关的参数
public class ResourceProperties {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
    private String[] staticLocations;
    private boolean addMappings;
    private final ResourceProperties.Chain chain;
    private final ResourceProperties.Cache cache;

1.2 webjars,请求为/webjars/**

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

webjars官网

1.2.1 测试
  • 访问http://localhost:8080/webjars/jquery/3.3.1-1/jquery.js
  • 所有 /webjars/**的请求 ,都在 classpath:/META-INF/resources/webjars/ 中获取资源

路径

测试webjars

1.3 请求为 /**

1.3.1 静态资源路径
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
  • 访问localhost:8080/xxx
    如果该请求项目未处理,就到静态资源路径获取xxx资源

1.4 欢迎页

  • org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
	// getStaticPathPattern : 静态资源映射(/**)
	return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
}
  • 静态资源文件夹下的所有index.html页面;被"/**"映射
    欢迎页
1.4.1 测试

访问:http://localhost:8080
结果:404

1.4.2 添加首页

在resource中添加public或static,添加index.html,再次访问

  • index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
看什么?6;你和我,走一波!
</body>
</html>

1.5 自定义图标

  • org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
// 使用spring.mvc.favicon.enabled启用自定义图标
// matchIfMissing : 不配置也默认启用
@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);
			// 所有的 **/favicon.ico请求都映射到faviconRequestHandler
			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.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
			List<Resource> locations = new ArrayList(staticLocations.length + 1);
			Stream var10000 = Arrays.stream(staticLocations);
			ResourceLoader var10001 = this.resourceLoader;
			this.resourceLoader.getClass();
			var10000.map(var10001::getResource).forEach(locations::add);
			locations.add(new ClassPathResource("/"));
			return Collections.unmodifiableList(locations);
		}
	}

图标

自定义图标

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

更换图标

1.6 自定义静态资源文件夹

  • application.properties
# 自定义静态资源文件夹
spring.resources.static-locations=classpath:/static,classpath:/public
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值