带有Spring和Angular的HTML5路由 [英] Html5 routing with Spring and Angular
问题描述
我正在尝试在某个时候,我需要使用这样的控制器将所有角度路线重定向到基本路径:
At some point I need to redirect all the angular routes to the base path with a controller like this:
@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:/";
}
}
正则表达式与大多数URL匹配,例如仪表板,搜索等:
While the regular expression matches most of the URLS, like dashboard, search etc:
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
Others are ignored and not matched at all, like 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中,则它与我想要的不匹配.有人知道如何使它起作用,或者甚至更好地在没有正则表达式的情况下如何实现它吗?
The regular expression seems to be fine if you test it in regexplanet , but it does not match what I want if I put it in the @RequestMapping. Does anybody know how to make it work, or even better how to do it without regular expressions?
推荐答案
The problem and solution is described here. The solution consist in the implementation of a OncePerRequestFilter where you can match the full URI against whatever you want.
这篇关于带有Spring和Angular的HTML5路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!