springboot静态资源

映射规则

@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties implements ResourceLoaderAware {
    //可以设置和静态资源有关的参数 eg:缓存时间等
public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Integer cachePeriod = this.resourceProperties.getCachePeriod();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(cachePeriod));
                }

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

            }
        }
  • 所有 /webjars/**请求 都去 classpath:/META-INF/resources/webjars/ 找资源;

    webjars:以jar包的方式引入静态资源;webjars

    localhost:8080/webjars/jquery/3.4.1/jquery.js

    <dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.4.1</version>
    </dependency>
  • "/**" 访问当前项目的任何资源,(静态资源的文件夹)

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

    localhost:8080/abc === 没人处理会去静态资源文件夹找abc

  • 欢迎页

    静态资源文件夹下所有的index.html页面;被 "/" 映射

    private String[] getStaticWelcomePageLocations() {
          String[] result = new String[this.staticLocations.length];
    
          for(int i = 0; i < result.length; ++i) {
              String location = this.staticLocations[i];
              if (!location.endsWith("/")) {
                  location = location + "/";
              }
    
              result[i] = location + "index.html";
          }
    
          return result;
      }

    localhost:8080/ ====默认找index.html页面

  • 网站图标

    所有的 **/favicon.ico 都是在静态资源文件夹下找;

    @Configuration
    @ConditionalOnProperty(
      value = {"spring.mvc.favicon.enabled"},
      matchIfMissing = true
    )
    public static class FaviconConfiguration {
              private final ResourceProperties resourceProperties;
    
              public FaviconConfiguration(ResourceProperties resourceProperties) {
                  this.resourceProperties = resourceProperties;
              }
    
              @Bean
              public SimpleUrlHandlerMapping faviconHandlerMapping() {
                  SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
                  mapping.setOrder(-2147483647);
                  mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
                  return mapping;
              }
    
              @Bean
              public ResourceHttpRequestHandler faviconRequestHandler() {
                  ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
                  requestHandler.setLocations(this.resourceProperties.getFaviconLocations());
                  return requestHandler;
              }
          }

您可能还喜欢...

发表评论

电子邮件地址不会被公开。 必填项已用*标注