Spring Boot-web开发 webjars与静态资源映射规则

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Java_Glory/article/details/89669606

1.首先创建一个Spring Boot应用的web工程 , 具体步骤参照 : IEDA快速创建Spring-Boot应用

2.Spring Boot对静态资源的映射规则:

     Ctrl+N搜索 : WebMvcAutoConfiguration

     

     再Ctrl+F搜索方法 : addResourceHandlers , 可以看到下面

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

            }
        }

     按住Ctrl键点击 resourceProperties

@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties implements ResourceLoaderAware, InitializingBean {
     //可以设置和静态资源有关的参数,缓存时间等
     .....
}

   (1)所有/webjars/** , 都去 classpath:/META-INF/resources/webjars/ 找资源

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

     

        <!--引入jQuery-webjar-->
        <dependency>
            <groupId>org.webjars.bower</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>

     

     访问的时候只需要写webjars下面资源的路径(各版本之间路径略有差异) : localhost:8080/webjars/jquery/3.3.1/dist/jquery.js

     (2)"/**"访问当前项目的任何资源 , (静态资源的文件夹)

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

     按住Ctrl键点击:    

 

public class ResourceProperties implements ResourceLoaderAware, InitializingBean {
    private static final String[] SERVLET_RESOURCE_LOCATIONS = new String[]{"/"};
    //在以下文件夹中加载资源
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
    private static final String[] RESOURCE_LOCATIONS;
    private String[] staticLocations;
    private Integer cachePeriod;
    private boolean addMappings;
    private final ResourceProperties.Chain chain;
    private ResourceLoader resourceLoader;
    ...........
}

     以这三个静态资源文件夹为例 : localhost:8080/xxx ===去静态资源文件夹里面找xxx

     (3)欢迎页 : 静态资源文件夹下的所有index.html页面 ; 被"/**"映射 ;

          localhost:8080/     找index页面

//配置欢迎页映射
@Bean
public WebMvcAutoConfiguration.WelcomePageHandlerMapping welcomePageHandlerMapping(ResourceProperties resourceProperties) {
            return new WebMvcAutoConfiguration.WelcomePageHandlerMapping(resourceProperties.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
        }

                                                                              ↓↓

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

     在publi文件夹下创建一个index.html文件 :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>Welcome</h1>
</body>
</html>

     运行项目后访问 : localhost:8080/

访问成功!

     (4)所有的 **/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;
            }
        }

     网站制作一个喜欢的图标 , 放在public文件夹里 :

 

     然后重新启动一下项目访问 : localhost:8080

图标就配置成功了!

也可以在配置文件中修改静态资源文件夹路径:

     多个路径用 "," 隔开

定义静态资源文件夹后 , 知识点(2)的路径就不生效了

猜你喜欢

转载自blog.csdn.net/Java_Glory/article/details/89669606