【问题标题】:Serving single html page with Spring Boot使用 Spring Boot 提供单个 html 页面
【发布时间】:2017-03-10 17:14:48
【问题描述】:

我正在开发一个使用 Spring Boot Web 的简单 Spring Boot 应用程序。根据 Spring Boot 文档,我将 index.html 放入资源目录的模板子目录中。我在 application.properties 文件中定义了:

spring.mvc.view.prefix=classpath:/templates/

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' 的 servlet 中名称为 'index' 的视图”。

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

亲切的问候, 戴夫

【问题讨论】:

  • 用@Controller注释类

标签: spring spring-boot


【解决方案1】:

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

  • index.html转成src/main/resources/static/index.html
  • application.properties 中删除属性
  • 删除addViewControllers方法和@EnableWebMvc注解——实际上你可以删除整个类
  • 通过http://localhost:8080/访问索引

【讨论】:

  • Cheers Maciej 但我已经尝试了上述建议,现在产生的错误是:No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'dispatcherServlet'
  • 您是否也从配置中删除了 @EnableWebMvc 注释?我更新了答案。
猜你喜欢
  • 2016-06-14
  • 2016-02-11
  • 2018-12-20
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多