springboot笔记(web篇)

1、webjars 以jar包/maven依赖引入静态文件

//读取静态文件方法
WebMvcAuotConfiguration.addResourceHandlers()
//读取路径
customizeResourceHandlerRegistration(registry
						.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod))
						.setCacheControl(cacheControl));

2、其他静态文件

//读取静态文件方法
WebMvcAuotConfiguration.addResourceHandlers()
//读取路径
if (!registry.hasMappingForPattern(staticPathPattern)) {
	customizeResourceHandlerRegistration(
		registry.addResourceHandler(staticPathPattern)
				.addResourceLocations(getResourceLocations(
					this.resourceProperties.getStaticLocations()))
				.setCachePeriod(getSeconds(cachePeriod))
				.setCacheControl(cacheControl));
}

 代码结构图

 所以静态资源读取路径为

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
			"classpath:/META-INF/resources/",
			"classpath:/resources/",
			"classpath:/static/",
			"classpath:/public/"
		};

 

3、获取欢迎页(WebMvcAutoConfiguration.welcomePageHandlerMapping() )

public class WebMvcAutoConfiguration {

    //.........

	private Optional<Resource> getWelcomePage() {
		String[] locations = getResourceLocations(
				this.resourceProperties.getStaticLocations());//获取路径
		return Arrays.stream(locations).map(this::getIndexHtml)//获取文件名
				.filter(this::isReadable).findFirst();
	}

	private Resource getIndexHtml(String location) {
		return this.resourceLoader.getResource(location + "index.html");
	}
}

//具体路径
public class ResourceProperties{
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
		"classpath:/META-INF/resources/",
		"classpath:/resources/",
		"classpath:/static/",
		"classpath:/public/" 
	};
}

 

4、图标映射

Spring Boot 会在静态资源目录下 与 根路径(按该顺序) 查找  favicon.ico 文件;
如果存在这样的文件,Spring Boot 会自动将其设置为应用图标

具体路径:

classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
/: 当前项目根路径下  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值