播放符合所有条件的框架路线 [英] play framework route that matches all

查看:76
本文介绍了播放符合所有条件的框架路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Play框架为我的休息服务开发一个有角度的应用程序.公用文件夹中的所有内容都是一个有角度的应用程序(样式表,javascript,图像和html).我希望每个不是针对样式表,javascript,templates或images文件夹中内容的请求都被路由到index.html页面.这样一来,角度路由就可以接管那里了.

I'm working on an angular app using play framework for my rest-services. Everything in the public folder is an angular app (stylesheets, javascripts, images and html). I want every request that is not for something in the stylesheets, javascripts, templates or images folder to be routed to the index.html page. This is so that angular routing can take over from there...

作为旁注,我可以提到我将把每个restservice放在/services/下,该链接链接到我自己的java控制器.

As a side note i can mention that I am going to place every restservice under /services/ which links to my own java controllers.

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

Is it possible in play framework 2.3.4 to define a route that catches all without having to use the matching elements?

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

This is my route defs so far:

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")

推荐答案

不可能省略匹配元素​​的使用,但是您可以通过控制器路由客户端.路由定义如下:

It's not possible to omit usage of matching elements but you can route a client via controller. The route definition looks like this:

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

相应的控制器可以实现如下:

And the corresponding controller can be implemented as follows:

public class Application extends Controller {

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

}

更新

如果您不想重定向客户端,则可以将静态资源作为流返回.在这种情况下,必须提供响应MIME类型.

If you don't want to redirect a client you can return a static resource as a stream. In this case a response MIME type is required.

public class Application extends Controller {

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

}

这篇关于播放符合所有条件的框架路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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