在 Spring Boot 中,可以使用 Spring Boot 配置静态资源文件目录,并且默认打开 index.html 文件。

首先,在 Spring Boot 的配置文件 (application.properties 或 application.yml) 中配置静态资源文件目录:

application.properties:

spring.resources.static-locations=classpath:/wwwroot/

application.yml:

spring:
  resources:
    static-locations: classpath:/wwwroot/

这里将静态资源文件目录设置为 classpath:/wwwroot/,表示在 classpath 下的 wwwroot 目录中查找静态资源文件。

然后,在 Spring Boot 的 Controller 中添加一个方法,将请求重定向到 index.html 文件:

@Controller
public class HomeController {

    @RequestMapping("/")
    public String home() {
        return "redirect:/index.html";
    }

}

这里定义了一个 @RequestMapping 注解,将请求 / 重定向到 index.html 文件。

最后,在 wwwroot 目录下添加一个 index.html 文件,即可在浏览器中访问 http://localhost:8080/,自动打开 wwwroot 下的 index.html 文件。

Spring Boot 配置 wwwroot 静态文件目录并默认打开 index.html

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

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