使用Spring Boot服务单个html页面



我正在开发一个使用Spring Boot Web的简单Spring Boot应用程序。按照Spring Boot文档的要求,我将index.html放入资源目录的templates子目录中。在应用程序中定义的。属性文件:

spring.mvc.view.prefix =类路径:/模板/

spring.mvc.view.suffix = . html

我的WebMvcConfigurerAdapter是这样的:

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
    super.addViewControllers(registry);
}   
}

我的问题是我似乎不能在http://localhost:8080服务index.html。我得到以下错误:"javax.servlet。ServletException:无法解析名称为dispatcherServlet中名称为index的视图".

在不使用控制器的情况下如何解决这个问题?

亲切的问候,戴夫

为了从Spring Boot应用程序提供静态内容,您只需要将它们放在src/main/resources/static中-不需要其他配置。

  • index.html放到src/main/resources/static/index.html
  • 删除application.properties属性
  • 删除addViewControllers方法和@EnableWebMvc注释-您实际上可以删除整个类
  • 通过到http://localhost:8080/
  • 访问索引

最新更新