Springboot/Angular2-如何处理HTML5网址? [英] Springboot/Angular2 - How to handle HTML5 urls?

查看:176
本文介绍了Springboot/Angular2-如何处理HTML5网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I believe this is a simple question, but I couldn't find an answer or at least use the correct terms in the search.

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

I am setting up Angular2 and Springboot together. By default, Angular will use paths like localhost:8080\dashboard and localhost:8080\dashboard\detail.

如果可能的话,我想避免将path用作哈希.正如Angular 文档所述:

I'd like to avoid using path as hashs, if possible. As Angular documentation states:

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

The router's provideRouter function sets the LocationStrategy to the PathLocationStrategy, making it the default strategy. We can switch to the HashLocationStrategy with an override during the bootstrapping process if we prefer it.

然后...

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

Almost all Angular 2 projects should use the default HTML 5 style. It produces URLs that are easier for users to understand. And it preserves the option to do server-side rendering later.

问题在于,当我尝试访问localhost:8080\dashboard时,Spring会寻找一些映射到此路径的控制器,而该路径不会.

The issue is that when I try to access localhost:8080\dashboard, Spring will look for some controller mapping to this path, which it won't have.

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

起初我以为我的所有服务都在localhost:8080\api下,而我的所有静态都在localhost:8080\app下.但是,如何告诉Spring忽略对此app路径的请求?

I thought initially to make all my services to be under localhost:8080\api and all my static under localhost:8080\app. But how do I tell Spring to ignore requests to this app path?

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

Is there a better solution with either Angular2 or Boot?

推荐答案

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

I have a solution for you, you can add a ViewController to forward requests to Angular from Spring boot.

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","/关于","/测试","/测试/新","/测试/**","/问题","/答案")发送到我的SPA 您可以对自己的预言进行相同的操作,还可以将404错误页面重定向到索引页面,作为下一步. 享受吧!

here I have redirected all my angular2 ("/bikes", "/milages", "/gallery", "/tracks", "/tracks/{id:\w+}", "/location", "/about", "/tests","/tests/new","/tests/**","/questions","/answers") to my SPA You can do the same for your preject and you can also redirect your 404 error page to the index page as a further step. Enjoy!

这篇关于Springboot/Angular2-如何处理HTML5网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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