gpt4 book ai didi

angularjs - 匹配所有的 Play Framework 路线

转载 作者:行者123 更新时间:2023-12-03 14:12:06 24 4
gpt4 key购买 nike

我正在为我的休息服务使用 Play Framework 开发一个有 Angular 的应用程序。 public 文件夹中的所有内容都是一个有 Angular 的应用程序(样式表、javascripts、图像和 html)。我希望所有不是针对样式表、javascripts、模板或图像文件夹中的内容的请求都路由到 index.html 页面。这是为了让 Angular 路由可以从那里接管......

作为旁注,我可以提到我将把每个休息服务放在 /services/ 下。它链接到我自己的 java Controller 。

是否可以在 play 框架 2.3.4 中定义一条无需使用匹配元素即可捕获所有内容的路由?

到目前为止,这是我的路线定义:

GET     /                       controllers.Assets.at(path="/public", file="index.html")
GET /stylesheets/*file controllers.Assets.at(path="/public/stylesheets", file)
GET /javascripts/*file controllers.Assets.at(path="/public/javascripts", file)
GET /templates/*file controllers.Assets.at(path="/public/templates", file)
GET /images/*file controllers.Assets.at(path="/public/images", file)

#this line fails
GET /* controllers.Assets.at(path="/public", file="index.html")

最佳答案

不可能省略匹配元素​​的使用,但您可以通过 Controller 路由客户端。路由定义如下所示:

GET         /*path               controllers.Application.matchAll(path)

相应的 Controller 可以实现如下:
public class Application extends Controller {

public static Result matchAll(String path) {
return redirect(controllers.routes.Assets.at("index.html"));
}

}

更新

如果您不想重定向客户端,您可以将静态资源作为流返回。在这种情况下,需要响应 MIME 类型。
public class Application extends Controller {

public static Result matchAll(String path) {
return ok(Application.class.getResourceAsStream("/public/index.html")).as("text/html");
}

}

关于angularjs - 匹配所有的 Play Framework 路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25686617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com