Spring Boot 2.x实战47 - Spring Web MVC 19 - Web MVC配置(常用外部配置:静态文件目录,欢迎页,Favicon)

3.11 其他外部属性配置
3.11.1 静态文件目录

Web开发中有很多静态的文件,如:html、js、css、图片等。在Spring Boot下,下列目录都可放置静态文件:

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

当前的classpath类路径是Spring Boot为我们生成的src/main/resources目录。我们还可以通过spring.resources.static-locations来重载覆盖默认的静态目录。

spring.resources.static-locations: classpath:/my-static/,classpath:/META-INF/resources/, classpath:/resources/,classpath:/static/, classpath:/public/

当前所有的静态文件目录:

在这里插入图片描述
我们可以直接访问这些静态文件:
在这里插入图片描述
Spring Boot还支持对webjar(将web库打进jar包,https://www.webjars.org/)中的静态文件(classpath:/META-INF/resources/webjars/映射成/webjars/**)的支持,如我们添加Bootstrap的webjar到build.gradle

implementation 'org.webjars:bootstrap:4.3.1'

在这里插入图片描述
可以通过访问http://localhost:8080/webjars/bootstrap/4.3.1/css/bootstrap.css来访问:
在这里插入图片描述

3.11.2 欢迎页

当我们访问应用的根路径的时候,Spring Boot会在静态文件目录里寻找index.html文件作为欢迎页。如我们放置index.html放置到static目录下。
在这里插入图片描述
我们访问http://localhost:8080/即可打开index.html
在这里插入图片描述

3.12.3 Favicon

Spring Boot会寻找静态文件目录文件下的favicon.ico文件,我们生成(https://www.favicon-generator.org/)一个favicon.ico文件放置在static下。
在这里插入图片描述
这是浏览器Tab页的小图标变成了我们定制的图标:

在这里插入图片描述

新书推荐:

我的新书《从企业级开发到云原生微服务:Spring Boot 实战》已出版,内容涵盖了丰富Spring Boot开发的相关知识
购买地址:https://item.jd.com/12760084.html
在这里插入图片描述

主要包含目录有:

第一章 初识Spring Boot(快速领略Spring Boot的美丽)
第二章 开发必备工具(对常用开发工具进行介绍:包含IntelliJ IDEA、Gradle、Lombok、Docker等)
第三章 函数式编程
第四章 Spring 5.x基础(以Spring 5.2.x为基础)
第五章 深入Spring Boot(以Spring Boot 2.2.x为基础)
第六章 Spring Web MVC
第七章 数据访问(包含Spring Data JPA、Spring Data Elasticsearch和数据缓存)
第八章 安全控制(包含Spring Security和OAuth2)
第九章 响应式编程(包含Project Reactor、Spring WebFlux、Reactive NoSQL、R2DBC、Reactive Spring Security)
第十章 事件驱动(包含JMS、RabbitMQ、Kafka、Websocket、RSocket)
第11章 系统集成和屁股里(包含Spring Integration和Spring Batch)
第12章 Spring Cloud与微服务
第13章 Kubernetes与微服务(包含Kubernetes、Helm、Jenkins、Istio)
多谢大家支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值