如何使视图控制器忽略 server.servlet.context-path。

huangapple 未分类评论50阅读模式
标题翻译

How to make viewController ignore server.servlet.context-path

问题

以下是您提供的内容的翻译:

我对于从Spring Boot提供静态内容的配置如下。

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${frontend.location}")
    private String frontendLocation;

    @Override
    public void addViewControllers(ViewControllerRegistry reg) {
        reg.addViewController("/").setViewName("forward:/index.html");
        reg.addViewController("/{x:[\\w\\-]+}").setViewName("forward:/index.html");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(frontendLocation);
    }

}

只要没有配置 server.servlet.context-path,这个配置就可以正常工作。当配置了 server.servlet.context-path 时,它会作为URL的一部分传递给前端路由器。
如何使视图控制器忽略 server.servlet.context-path。
解决方案是不将上下文路径(context path)转发到 index.html。我该如何实现这一点?

英文翻译

I have the following configuration for serving static content from Spring Boot.

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        @Value("${frontend.location}")
        private String frontendLocation;
    
        @Override
        public void addViewControllers(ViewControllerRegistry reg) {
            reg.addViewController("/").setViewName("forward:/index.html");
            reg.addViewController("/{x:[\\w\\-]+}").setViewName("forward:/index.html");
        }
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**").addResourceLocations(frontendLocation);
        }
    
    }

This works fine as long as there is no server.servlet.context-path. When server.servlet.context-path is configured, it is being passed down to the frontend router as part of the URL.
如何使视图控制器忽略 server.servlet.context-path。
The solution would be not to forward context path to index.html. How can I achieve that?

huangapple
  • 本文由 发表于 2020年3月16日 19:47:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/60705424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定