简体   繁体   English

使用Spring和Angular进行Html5路由

[英]Html5 routing with Spring and Angular

I'm trying to implement HTML5 routing with Spring-boot and Angular 1.5, following this article . 我正在尝试使用Spring-boot和Angular 1.5实现HTML5路由,遵循本文

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:/";
  }
}

While the regular expression matches most of the URLS, like dashboard, search etc: 正则表达式匹配大多数URL,如仪表板,搜索等:

 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]

Others are ignored and not matched at all, like https://localhost:8443/faults/64539352 其他人被忽略,根本不匹配,例如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]

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. 如果你在regexplanet中测试它,正则表达式似乎没问题 ,但是如果我把它放在@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. 解决方案包括OncePerRequestFilter的实现,您可以在其中匹配完整的URI与您想要的任何内容。

You can use a correct PathMatcher to do the job: {path:(?:(?!api|.).)*}/** 您可以使用正确的PathMatcher来完成这项工作: {path:(?:(?!api|.).)*}/**

More info here 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM