2016-08-05 111 views
4

我想實現HTML5路由彈簧引導和角1.5,跟隨this articleHtml5路由與彈簧和角

在某些時候我需要所有的角路由到基座路徑重定向與這樣的控制器:

@Controller 
public class UrlController { 
    // Match everything without a suffix (so not a static resource) 
    @RequestMapping(value = "/{path:[^\\.]*}")) 
    public String redirect(HttpServletRequest request) { 
    // Forward to home page so that route is preserved. 
    return "forward:/"; 
    } 
} 

雖然正則表達式匹配大多數URLS的,如儀表盤,搜索等:

2016-08-05 16:39:58 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:306 - Looking up handler method for path /dashboard 
2016-08-05 16:39:58 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:313 - Returning handler method UrlController.redirect(javax.servlet.http.HttpServletRequest,java.lang.String)] 
2016-08-05 16:39:58 DEBUG o.s.b.f.s.DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'urlController' 
2016-08-05 16:39:58 DEBUG o.s.w.s.DispatcherServlet:947 - Last-Modified value for [/dashboard] is: -1 
2016-08-05 16:39:58 DEBUG c.a.a.w.i.LoggingInterceptor:22 - Start processing url request: https://localhost:8443/dashboard 
redirecting to home page from url https://localhost:8443/dashboard 
2016-08-05 16:39:58 DEBUG c.a.a.w.i.LoggingInterceptor:35 - Finished processing url request: https://localhost:8443/dashboard, duration: 0[ms] 

其他被忽略,且不匹配可言,就像https://localhost:8443/faults/64539352

2016-08-05 17:00:05 DEBUG o.s.w.s.DispatcherServlet:861 - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/faults/64539352] 
2016-08-05 17:00:05 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:306 - Looking up handler method for path /faults/64539352 
2016-08-05 17:00:05 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:316 - Did not find handler method for [/faults/64539352] 

如果你在regexplanet 中測試它,那麼正則表達式似乎沒問題,但如果我把它放在@RequestMapping中,它就不符合我想要的。 有沒有人知道如何使它工作,甚至更好的如何做到沒有正則表達式?

+0

問題似乎是當您使用第二個「/」,服務器/東西是好的,但服務器/東西/任何東西在任何情況下都不匹配 –

回答

1

該問題和解決方案描述爲here。該解決方案包含一個OncePerRequestFilter的實現,您可以在其中匹配完整的URI與任何你想要的。