angularjs - 匹配所有的 Play Framework 路线

标签 angularjs routing playframework-2.0

我正在为我的休息服务使用 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/

相关文章:

javascript - AngularJS 路由包括多个 JS 文件和目录

javascript - Angular $http.query 使用数组作为参数?

windows - Socket.Bind 和 IP 源路由,具有多个本地网络接口(interface)

ruby-on-rails - 资源丰富的路由,但使用 GET 而不是 POST 的 #create 操作

java - 如何在 play 2.0.1 中更改上传文件的目录?

jquery - 获取ImageUri的文件内容

javascript - 检测手动更改查询字符串angularjs

Angular 路由器链接事件嵌套菜单

java - Play : Custom Ebean constraints

scala - Play 迭代器 : error for simple file iteration