spring mvc捕获所有路径,但仅未知路径 [英] spring mvc catch all route but only unknown routes

查看:65
本文介绍了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.

这篇关于spring mvc捕获所有路径,但仅未知路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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