Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧靴不';t将文件夹请求映射到'index.html'文件_Java_Static_Spring Boot - Fatal编程技术网

Java 弹簧靴不';t将文件夹请求映射到'index.html'文件

Java 弹簧靴不';t将文件夹请求映射到'index.html'文件,java,static,spring-boot,Java,Static,Spring Boot,我有一个结构如下的static文件夹: index.html docs/index.html Spring Boot将请求/正确映射到index.html。但是它不能将/docs/请求映射到/docs/index.html(/docs/index.html请求工作正常) 如何将文件夹/子文件夹请求映射到相应的index.html文件?这不是Spring引导到index.html的映射,而是servlet引擎(这是一个欢迎页面)。只有一个欢迎页面(根据规范),目录浏览不是容器的一项功能。您可以手动

我有一个结构如下的
static
文件夹:

index.html
docs/index.html

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


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

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

您可以手动添加视图控制器映射以实现此功能:

@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

默认情况下,Spring boot show index.html

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

例如:

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-static
Java8推出后,
WebMVCConfigureAdapter
在Spring5/SpringBoot2中被弃用

现在使用它将提高:

WebMVCConfigureAdapter类型已弃用

因此,为了重新发挥作用,我们需要对其进行如下更改:

@配置
公共类CustomWebMVCConfiguer实现WebMVCConfiguer{
@凌驾
public void addViewController(ViewControllerRegistry注册表){
registry.addViewController(“/docs”).setViewName(“重定向:/docs/”);
registry.addViewController(“/docs/”).setViewName(“转发:/docs/index.html”);
}
}

这解释了问题,但没有提出解决方案,因此我不能接受这个答案。我认为除了编写自己的目录浏览器servlet处理程序之外,没有其他解决方案。如果您愿意,我可以建议这样做?我建议的视图控制器映射是否回答了您的问题?如果是,请接受。否则,请澄清您的问题,我很乐意更新我的答案。也适用于嵌套子文件夹:
registry.addViewController(“/v2/docs”)。setViewName(“重定向:/v2/docs/”
registry.addViewController(“/v2/docs/”).setViewName(“转发:/v2/docs/index.html”)很好的答案,谢谢!我已经对其进行了更新,使其能够使用。是否有办法使其能够使用所有子文件夹,而不仅仅是命名的子文件夹?是的,您可以添加映射,但假设您使用Hugo()生成了一个静态博客页面,并从Spring静态文件夹提供服务。Hugo的每个博客条目都有一个index.html。因此,如果你的博客有100个页面,你将需要100个映射。此外,每次你添加一个新的博客项目,你都必须为它添加一个映射。不太方便。