在 Spring Boot 中,可以通过在 application.properties 文件中配置静态资源目录和默认打开的文件。

  1. 静态资源目录配置

可以通过设置 spring.resources.static-locations 属性来配置静态资源目录,多个目录可以用逗号分隔。例如:

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

上述配置表示将静态资源放在 classpath 下的 static 和 public 目录中。

  1. 默认打开的文件配置

可以通过设置 spring.mvc.static-path-pattern 属性来配置默认打开的文件。例如:

spring.mvc.static-path-pattern=/index.html

上述配置表示默认打开的文件是 index.html。如果请求的路径是 /,则会自动访问 index.html 文件。

注意:静态资源的访问路径是相对于 classpath 的,而不是相对于项目根目录的。因此,如果静态资源放在 src/main/resources/static 目录下,则访问路径应该是 /xxx.xxx(xxx.xxx 是静态资源文件名)。

Spring Boot 静态资源目录配置及默认打开文件设置

原文地址: https://www.cveoy.top/t/topic/oIJG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录