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

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

问题描述

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

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

如果可能,我想避免使用路径作为哈希值.正如 Angular 文档 所述:><块引用>

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

然后……

<块引用>

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

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

白标错误页面出现意外错误(类型=未找到,状态=404).没有可用的消息

我最初想将我的所有服务都放在 localhost:8080api 下,并将所有静态服务放在 localhost:8080app 下.但是我如何告诉 Spring 忽略对这个 app 路径的请求?

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

解决方案

我有一个解决方案,你可以添加一个 ViewController 来将请求从 Spring boot 转发到 Angular.

import org.springframework.stereotype.Controller;导入 org.springframework.web.bind.annotation.RequestMapping;@控制器公共类视图控制器{@RequestMapping({ "/bikes", "/milages", "/gallery", "/tracks", "/tracks/{id:\w+}", "/location", "/about", "/tests","/tests/new","/tests/**","/questions","/answers" })公共字符串索引(){返回转发:/index.html";}}

这里我已经重定向了我所有的 angular2(/bikes"、/milages"、/gallery"、/tracks"、/tracks/{id:w+}"、/location"、/about", "/tests","/tests/new","/tests/**","/questions","/answers") 到我的 SPA你可以对你的项目做同样的事情,你也可以将你的 404 错误页面重定向到索引页面作为进一步的步骤.享受!

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

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

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

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.

And then...

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.

The issue is that when I try to access localhost:8080dashboard, 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

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

Is there a better solution with either Angular2 or Boot?

解决方案

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

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 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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