【问题标题】:Spring boot doesn't map folder requests to `index.html` filesSpring Boot 不会将文件夹请求映射到 `index.html` 文件
【发布时间】:2015-04-10 19:38:29
【问题描述】:

我有 static 具有以下结构的文件夹:

index.html
docs/index.html

Spring Boot 正确地将请求 / 映射到 index.html。但它不会将/docs/ 请求映射到/docs/index.html/docs/index.html 请求正常工作)。

如何将文件夹/子文件夹请求映射到适当的index.html 文件?

【问题讨论】:

  • 我建议的视图控制器映射是否回答了您的问题?如果是,请接受。否则,请澄清您的问题,我很乐意更新我的答案。

标签: java static spring-boot


【解决方案1】:

您可以手动添加视图控制器映射来完成这项工作:

@Configuration
public class CustomWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {

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

如果/docs(不带斜杠)被请求,第一个映射会导致 Spring MVC 向客户端发送重定向。如果您在 /docs/index.html 中有相关链接,这是必要的。第二个映射在内部将任何请求转发到/docs/(不向客户端发送重定向)到docs 子目录中的index.html

【讨论】:

  • 也适用于嵌套子文件夹:registry.addViewController("/v2/docs").setViewName("redirect:/v2/docs/");registry.addViewController("/v2/docs/").setViewName("forward:/v2/docs/index.html");
  • 很好的答案,谢谢!我已对其进行了更新以使其与 Spring 5 / Spring Boot 2 一起使用
  • 有没有办法让所有子文件夹,而不仅仅是命名文件夹?
  • 是的,您可以添加映射,但假设您使用 Hugo (gohugo.io) 生成了一个静态博客页面,并从 Spring 静态文件夹中提供它。 Hugo 的每个博客条目都有一个 index.html。因此,如果您的博客有 100 个页面,您将需要 100 个映射。此外,每次添加新的博客项目时,都必须为此添加映射。不是很方便。
【解决方案2】:

在 Java 8 引入 Default Methods in Interfaces 之后,WebMvcConfigurerAdapter在 Spring 5 / Spring Boot 2 中被弃用

现在使用它会引发the warning

类型 WebMvcConfigurerAdapter 已弃用

因此,要让@hzpz's solution 再次工作,我们需要进行如下更改:

@Configuration
public class CustomWebMvcConfigurer implements WebMvcConfigurer {

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

【讨论】:

    【解决方案3】:

    这不是 Spring Boot 到 index.html 的映射,而是 servlet 引擎(这是一个欢迎页面)。只有一个欢迎页面(根据规范),目录浏览不是容器的功能。

    【讨论】:

    • 这解释了问题,但没有提出解决方案,所以我不能接受这个答案
    • 我认为除了编写自己的目录浏览器 servlet 处理程序之外没有其他解决方案。如果你愿意,我可以建议?
    【解决方案4】:

    如果找不到静态资源,这个总是会尝试匹配requestPath + /index.html

    import java.util.List;
    import javax.servlet.http.HttpServletRequest;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.Ordered;
    import org.springframework.core.io.Resource;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.springframework.web.servlet.resource.PathResourceResolver;
    import org.springframework.web.servlet.resource.ResourceResolverChain;
    
    @Configuration
    public class ResourceHandlerConfig implements WebMvcConfigurer {
    
      static class IndexFallbackResourceResolver extends PathResourceResolver{
        @Override
        protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
            List<? extends Resource> locations, ResourceResolverChain chain) {
          Resource resource = super.resolveResourceInternal(request, requestPath, locations, chain);
          if(resource==null){
            //try with /index.html
            resource = super.resolveResourceInternal(request, requestPath + "/index.html", locations, chain);
          }
          return resource;
        }
      }
    
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .setOrder(Ordered.LOWEST_PRECEDENCE)
            .addResourceHandler("/**")
            .addResourceLocations("classpath:/static/")
            //first time resolved, that route will always be used from cache
            .resourceChain(true)
            .addResolver(new IndexFallbackResourceResolver());
      }
    }
    

    【讨论】:

      【解决方案5】:

      Spring boot 默认显示 index.html。

      但 index.html 应该是 在 /资源/静态或 /公开

      示例:

      https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-static
      

      【讨论】:

        猜你喜欢
        • 2017-05-01
        • 2019-11-25
        • 2016-06-27
        • 2015-06-19
        • 2013-05-12
        • 2021-12-18
        • 2018-01-02
        • 2021-04-19
        • 2015-12-05
        相关资源
        最近更新 更多