小编典典

Springboot / Angular2-如何处理HTML5网址?

spring

我相信这是一个简单的问题,但我找不到答案,或者至少在搜索中使用正确的字词。

我正在设置Angular2Springboot在一起。默认情况下,Angular将使用localhost:8080\dashboard和之类的路径localhost:8080\dashboard\detail

如果可能的话,我想避免使用path作为哈希值。如Angular 文档所述:

路由器的ProvideRouter函数将LocationStrategy设置为PathLocationStrategy,使其成为默认策略。如果愿意,我们可以在引导过程中使用覆盖切换到HashLocationStrategy

然后…

几乎所有Angular 2项目都应使用默认的HTML 5样式。它产生的URL更易于用户理解。并且保留了以后进行服务器端渲染的选项。

问题是,当我尝试访问时localhost:8080\dashboard,Spring会寻找一些映射到此路径的控制器,而这是没有的。

Whitelabel Error Page
There was an unexpected error (type=Not Found, status=404).
No message available

我最初以为是将我的所有服务都置于底层,localhost:8080\api而将我所有的静态置于底层localhost:8080\app。但是,如何告诉Spring忽略对此app路径的请求?

Angular2或Boot是否有更好的解决方案?


阅读 414

收藏
2020-04-11

共1个答案

小编典典

我为你提供了一个解决方案,你可以添加一个ViewController以将请求从Spring引导转发到Angular。

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ViewController {

@RequestMapping({ "/bikes", "/milages", "/gallery", "/tracks", "/tracks/{id:\\w+}", "/location", "/about", "/tests","/tests/new","/tests/**","/questions","/answers" })
   public String index() {
       return "forward:/index.html";
   }
}

在这里,我重定向了我所有的angular2(“ / bikes”,“ / milages”,“ / gallery”,“ / tracks”,“ / tracks / {id:\ w +}”,“ / location”,“ / about”, “ / tests”,“ / tests / new”,“ / tests / **”,“ / questions”,“ / answers”)到我的SPA中。你可以对自己的预言进行相同的操作,也可以重定向404错误页面进入索引页面,作为下一步。请享用!

2020-04-11