【问题标题】:How to map index.html to root context in spring boot application?如何将 index.html 映射到 Spring Boot 应用程序中的根上下文?
【发布时间】:2019-05-13 10:09:27
【问题描述】:

我在我的 Spring Boot 应用程序的资源文件夹中创建了一个静态文件夹。在静态文件夹中,我有一个 index.html 文件。然后我将此添加到我的配置中:

protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/static/");
}

现在我可以在 localhost:8080/index.html 访问 index.html,但我想做的是在 localhost:8080 访问它。我已经试过了:

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

但我知道 ServletDispatcher 无法识别“forward:/index.html”。有人可以帮我弄这个吗?提前致谢。

编辑

我通过删除所有自定义配置解决了这个问题,因此我的应用可以使用默认配置。在我的特殊情况下,这对我有用,但知道如何使用自定义配置来实现这一点也不错。所以这个问题还是悬而未决,欢迎大家就这个问题提出一些意见。

【问题讨论】:

标签: spring spring-boot


【解决方案1】:

我认为没有必要指定扩展名和转发。你可以试试这个:

public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("index");
}

【讨论】:

  • 我也试过这个,但仍然出现同样的错误。我什至尝试在属性文件中配置 mvc-path,有各种不同的变体,但没有结果。
猜你喜欢
  • 2015-02-07
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 2015-04-10
  • 1970-01-01
相关资源
最近更新 更多