标题翻译
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的一部分传递给前端路由器。
解决方案是不将上下文路径(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.
The solution would be not to forward context path to index.html. How can I achieve that?
专注分享java语言的经验与见解,让所有开发者获益!
评论