06、SpringBoot实战:SpringBoot访问静态资源

本文详细解析Spring Boot项目中静态资源的存放与访问方式,涵盖默认的static和templates目录、其他可选资源位置以及如何通过配置自定义静态资源路径,帮助开发者高效管理Web资源。

Spring Boot 访问静态资源

    • 1.static 目录
  • 2.templates 目录
  • 3.静态资源存放其他位置
  • 4.自定义静态资源位置
  • 在 SpringBoot 项目中没有我们之前常规 web 开发的 WebContent(WebApp),它只有 src 目录。
  • 在 src/main/resources 下面有两个文件夹,static 和 templates。
  • SpringBoot 默认在 static 目录中存放静态页面,而 templates 中放动态页面。
     

1.static 目录

  • Spring Boot 通过 classpath/static 目录访问静态资源。注意存放静态资源的目录名称必须是 static。

2.templates 目录

  • 在 Spring Boot 中不推荐使用 jsp 作为视图层技术,而是默认使用 Thymeleaf 来做动态页面。
  • Templates 目录这是存放 Thymeleaf 的页面

3.静态资源存放其他位置

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

4.自定义静态资源位置

spring.resources.static-locations=classpath:/suibian/,classpath:/static/

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: