一、静态资源探究
  • stringboot 对于静态资源放置的位置有规定的
  • 在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)); } }

  

  • 可以读取的文件有的有
    • public ,static ,/** ,resourse localhost: 8080/
    • 优先级: resource > static(默认)> public

 

 

 

 

二、什么是webjars
 
  • 本质是以jar方式引入静态资源
  • 使用jQuery
    • 引入依赖
<dependency> <groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version> </dependency>

 

 

 

三.如何将web在stringboot体现出来呢
  1. 在static 文件下加入index.htm
    1. 写入web元素
  2. 在静态资源文件中,有一个欢迎页的Mappping

 

WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders, ApplicationContext applicationContext, Resource welcomePage, String staticPathPattern) { 
if (welcomePage != null && "/**" .equals(staticPathPattern)) {
// staticPathPattern 在静态文件路径下 logger.info("Adding welcome page: " + welcomePage);
this.setRootViewName("forward:index.html");
} else if (this.welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
logger.info("Adding welcome page template: index");
this.setRootViewName("index"); } }

  

private Resource getIndexHtml(Resource location) {
try { Resource resource = location.createRelative( "index.html" );
if (resource.exists() && resource.getURL() != null ) { return resource; }
}
catch (Exception var3) { } return null ; }

  

  • 所有的index.xml 页面;被/** 映射
所以:呈现一下 localhost: 8080
 

 

 

  1. stringboot 调页面
见下节——