2014-09-05 105 views
4

我正在爲我的休息服務使用遊戲框架的角度應用程序。公共文件夾中的所有內容都是角度應用程序(樣式表,JavaScript,圖像和HTML)。我希望每一個不是樣式表,javascripts,模板或圖片文件夾中的任何請求都被路由到index.html頁面。這是角度路由可以從那裏接管...玩框架路線,匹配所有

作爲一個側面說明,我可以提到,我將提出每個restservice下的鏈接到我自己的java控制器的/services/

在播放框架2.3.4中是否可以定義無需使用匹配元素即可捕獲所有路徑?

這是我的路線DEFS至今:

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

回答

3

這是不可能省略匹配元素​​的使用,但你可以通過控制器路由客戶端。路由定義是這樣的:

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

以及相應的控制器可以實現如下:

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

} 
+1

謝謝!曾嘗試類似的解決方案,但不知道如何返回一個靜態文件作爲結果! – 2014-09-05 16:37:49

+0

順便說一句,我認爲它應該'返回重定向(controllers.routes.Assets.at(「/ public」,「index.html」));' – 2014-09-05 16:40:24

+0

是否有可能返回index.html文件,但沒有重定向?重定向會導致url搞砸,然後angular.js路由不會正確觸發 – 2014-09-05 17:17:29

0

對於這個任務,你可以用Global classonHandlerNotFound這會使一些頁面重定向無:

import play.*; 
import play.mvc.*; 
import play.mvc.Http.*; 
import play.libs.F.*; 

import static play.mvc.Results.*; 

public class Global extends GlobalSettings { 

    public Promise<Result> onHandlerNotFound(RequestHeader request) { 
     return Promise.<Result>pure(notFound(
      views.html.notFoundPage.render(request.uri()) 
     )); 
    } 

}