Spring Boot不会将文件夹请求映射到`index.html`文件 [英] Spring boot doesn't map folder requests to `index.html` files

查看:82
本文介绍了Spring Boot不会将文件夹请求映射到`index.html`文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got static folder with following structure:

index.html
docs/index.html

index.html
docs/index.html

Spring Boot正确地将请求/映射到 index.html .但是,它不会将/docs/请求映射到/docs/index.html (/docs/index.html 请求正确运行).

Spring Boot correctly maps requests / to index.html. But it doesn't map /docs/ request to /docs/index.html (/docs/index.html request works correctly).

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

How to map folder/subfolder requests to appropriate index.html files?

推荐答案

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

You can manually add a view controller mapping to make this work:

@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

The first mapping causes Spring MVC to send a redirect to the client if /docs (without trailing slash) gets requested. This is necessary if you have relative links in /docs/index.html. The second mapping forwards any request to /docs/ internally (without sending a redirect to the client) to the index.html in the docs subdirectory.

这篇关于Spring Boot不会将文件夹请求映射到`index.html`文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆