SpringBoot Web开发

75 阅读1分钟

web自动配置规则

  1. WebMvcAutoConfiguration
  2. WebMvcProperties
  3. ViewResolver自动配置
  4. 静态资源自动映射
  5. Formatter与Converter自动配置
  6. HttpMessageConverter自动配置
  7. 静态首页
  8. favicon
  9. 错误处理

SpringBoot对于静态资源的映射规则

webMvcAutoConfiguration类的addResourceHandlers方法(添加资源的映射)

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
        Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
        CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
     if (!registry.hasMappingForPattern("/webjars/**")) {
        this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }

        String staticPathPattern = this.mvcProperties.getStaticPathPattern();
        if (!registry.hasMappingForPattern(staticPathPattern)) {
            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

    }
}
  •  所有的/webjars/**都要去classpath:/META-INF/resources/webjars/路径下找资源
  • webjars:以jar包的形式引入静态资源

资源映射

数组中的值在项目中的位置
classpath:/META-INF/resources/src/main/resources/META-INF/resources/
classpath:/resources/src/main/resources/resources/
classpath:/static/src/main/resources/static/
classpath:/public/src/main/resources/public/

 

网站图标映射

图标一般为favicon.ico格式的文件,一般都是在静态资源文件夹下