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)
多谢大家支持。