要将静态文件配置为根目录并默认打开index.html,可以在Spring Boot应用程序中添加以下代码:

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(0);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
    }
}

这将在classpath:/static/目录下查找所有静态文件,并将它们映射到根目录下。然后,它会将根路径"/"与index.html文件关联起来,以便在应用程序启动时默认打开该文件

spring root配置静态文件并且默认打开indexhtml

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

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