【spring boot笔记】6.(Web 开发)Spring Boot静态资源劲射规则与图标修改

创建一个Web项目时,我们只需要创建Spring Boot应用,选中需要的模块, Spring Boot会自动配置好所有的模块。最后我们再自己编写业务代码即可。


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

webjar资源
  • 所有的/webjar/** 都去classpath:/META-INF/resources/webjars/ 找资源
  • webjars:以jar包的方式引入静态资源

http://www.webjars.org/

静态资源文件夹路径

classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
/

其中classpath的根目录是resources:
【spring boot笔记】6.(Web 开发)Spring Boot静态资源劲射规则与图标修改
当我们访问localhost:8080/a_file时,默认就是从上面这几个路径中去找的。
同时当我们直接访问localhost:8080/时,会从上面几个路径中找index.html文件来渲染。

图标修改

图标就是浏览器左边的那个小图标:
【spring boot笔记】6.(Web 开发)Spring Boot静态资源劲射规则与图标修改
例如csdn的浏览器图标就是红色背景下的一个C


设置图标的位置在文件:WebMvcAutoConfiguration.class中。使用:
Ctrl+Shift+N可以全局搜索文件找到这个文件,其中设置图标部分的代码为:

@Bean
            public SimpleUrlHandlerMapping faviconHandlerMapping() {
                SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
                mapping.setOrder(-2147483647);
                mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
                return mapping;
            }

需要更换浏览器图标时,将图标命名为:favicon.ico,然后放在上面的静态路径文件夹中即可。