spring mvc捕获所有路径,但仅未知路径
我有一个前端带有角度的Spring Boot应用程序.
I've got a spring boot app with angular on the frontend.
我在html5模式下使用ui-router,我希望spring在所有未知路由上呈现相同的index.html.
I'm using ui-router with html5 mode and I would like spring to render the same index.html on all unknown routes.
// Works great, but it also overrides all the resources
@RequestMapping
public String index() {
return "index";
}
// Seems do be the same as above, but still overrides the resources
@RequestMapping("/**")
public String index() {
return "index";
}
// Works well but not for subdirectories. since it doesn't map to those
@RequestMapping("/*")
public String index() {
return "index";
}
所以我的问题是我如何创建后备映射,但可以让资源通过?
So my question is how can i create a fallback mapping but that lets through the resources?
我发现最简单的方法是实现自定义404页面.
The simplest way I found was implementing a custom 404 page.
@Configuration
public class MvcConfig {
@Bean
public EmbeddedServletContainerCustomizer notFoundCustomizer(){
return new NotFoundIndexTemplate();
}
private static class NotFoundIndexTemplate implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/"));
}
}
}
Neil McGuigan推广了HandlerInterceptor,但我不明白该如何实现.我不希望看到如何实现此功能,因为使用html5历史记录推送状态的单页应用程序将需要此行为.而且我还没有真正找到解决此问题的最佳方法.
Neil McGuigan propopes a HandlerInterceptor, but I wasn't able to understand how that would be implemented. I't would be great to see how this would be implemented, as single page applications using html5 history push state will want this behaviour. And I have not really found any best practices to this problem.