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));
}

 代码结构图

springboot笔记(web篇)_第1张图片

 所以静态资源读取路径为

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 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/
/: 当前项目根路径下  

 

你可能感兴趣的:(springboot,spring,boot)