Spring Boot 配置静态文件并默认打开 index.html
要将静态文件配置为根目录并默认打开'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'文件关联起来,以便在应用程序启动时默认打开该文件。
原文地址: https://www.cveoy.top/t/topic/oIKJ 著作权归作者所有。请勿转载和采集!