2012-03-20 45 views
32

我试图做一个特定的静态文件的路由,但我试图结束一个错误。路线到Play中的静态文件! 2.0

我做了3次不同的尝试:

1.

GET /file staticFile:/public/html/file.html 

的错误,我得到:

Compilation error 
string matching regex `\z' expected but `:' found 

2.

GET /file controllers.Assets.at(path="/public/html", "file.html") 

我得到的错误:

Compilation error 
Identifier expected 

3.

GET /file controllers.Assets.at(path="/public/html", file="file.html") 

我得到的错误:(这是最古怪的)

Compilation error 
not enough arguments for method at: (path: String, file: String)play.api.mvc.Call. Unspecified value parameter file. 

关于第三个错误怪异的部分是,它是在一个不同的文件抛出( app/views/main.scala.html)在以下行上:

<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")"> 

所有这些e方法在官方文档和/或线程中发现在stackoverflow上。 我在这里错过了什么?

谢谢。

+1

我认为第三个错误发生在默认模板中提到公共路由的第一个实例。我有完全相同的问题。我期望你/我尝试添加静态链接会扰乱反向路由。 – flurdy 2012-03-31 19:14:46

+0

什么?两年,没有被接受的答案。这个问题解决了吗? – Jus12 2014-10-05 16:19:14

+0

@ Jus12对不起,这只是我没有继续使用游戏(不是我的选择,这是不幸的),我不知道哪个答案是正确的。那个得票最多的人当时没有解决问题(正如我的评论所解释的)。如果有人会让我知道任何答案是否正确,我会很乐意将其标记为已接受。 – 2014-10-05 17:10:08

回答

18

IIRC,改变

<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")"> 

<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/", "main.css")"> 

我谈论你的第三次尝试

此外,注意额外/

编辑

GET /assets/main.css controllers.Assets.at(path="/public", file="/stylesheets/main.css") 

假设你的资源是/public/stylesheets/main.css

+2

这只是解决了另一个错误,但你改变的不是问题,当我注释掉这条路由线路时一切正常(除非该文件未被提供)。第三次尝试的错误是主要的模板文件,这正是我从_play新的app-name_命令中得到的,我没有改变它,并且正如我写的,没有这个路由,它运行良好。 – 2012-03-21 21:13:59

3

我遇到同样的问题,而我试图配置一些额外的CSS。它在“路由”文件中使用此语法

GET /css/*file      controllers.Assets.at(path="/public/css", file) 
+0

与上面相同,但它的工作原理,但我们似乎无法知道为什么 – BlueSky 2017-03-26 11:57:10

4

我有完全相同的问题。我遵循了@Jamil的建议,并设法使这个静态文件(在我的情况下是一个图标)工作,并设法让模板编译,但尝试使用视图时在运行时得到一个新的错误。下面相关的代码,

切换到路径(这条路线现在可以解决正确)

GET  /favicon.ico    controllers.Assets.at(path="/public/images", file="favicon.png") 

切换到视图(编译)

<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/","main.css")"> 

新的错误(仅在运行时)

[MatchError: (stylesheets/,main.css) (of class scala.Tuple2)] 

// @LINE:29 
// @LINE:28 
def at(path:String, file:String) = { 
    (path, file) match { 
// @LINE:28 
case (path, file) if path == "/public" => Call("GET", "/assets/" + implicitly[PathBindable[String]].unbind("file", file)) 

我知道这不是一个答案,但也许它会让某人得到一个答案。

+0

感谢您的答复,我没有办法尝试这个目前为止,我希望能回到它并将回报。 – 2012-08-02 08:11:33

+2

正确的路线是这样的: “GET /favicon.ico controllers.Assets.at(path =”/ public“,file =”images/favicon.ico“)” – AlexV 2013-12-31 04:56:27

13

我不能完全确定这是否是正确的,但是这是我们使用映射一个公用文件夹包含我们的图像,JavaScript ...等什么..

# Map static resources from the /public folder to the /assets URL path 
GET  /assets/*file    controllers.Assets.at(path="/public", file) 
+0

感谢您的回复,我无法尝试这到目前为止,我希望能回到它并将回报。 – 2012-08-02 08:11:42

+0

如果我将“public”文件夹重命名为“assets”,这似乎不起作用(使用'path =“/ assets”',...)。任何想法为什么? – 2014-10-14 02:38:37

+0

这适用于我在Play 2.5上,这是奇怪的 – BlueSky 2017-03-26 11:52:12

13

这种能力仍然没有据我所知,已被添加。在航线配置

GET /    controllers.StaticFile.html(file = "public/index.html") 
1

我碰到了同样的问题

object StaticFile extends Controller { 

    def html(file: String) = Action { 
    var f = new File(file) 

    if (f.exists()) 
     Ok(scala.io.Source.fromFile(f.getCanonicalPath()).mkString).as("text/html"); 
    else 
     NotFound 
    } 

} 

,然后:但是,如果有人需要的答案,作为一个选项,帮助控制器可以为这些目的而创建的。当我加入了第二controllers.Assets.at路线像

GET /assets/*file controllers.Assets.at(path="/public", file) 
GET /assets2/*file controllers.Assets.at(path="/public2", file) 

默认在main.scala.html @routes.Assets.at调用失败编译

Compilation error 
not enough arguments for method at: (path: String, file: String)play.api.mvc.Call. Unspecified value parameter file. 

这似乎是因为其中有多个隐式参数匹配对象。解决的办法是增加一个领先的位置参数:

href="@routes.Assets.at("/public","/css/main.css")" 

不幸的是,如果你回到其在路线只有一个资产线,你将不得不更改为一个参数的形式,以避免编译错误。对我来说就像一个错误。

10

最干净的解决方案是创建您自己的AssetsBuilder,它将构建您的静态页面。

在控制器包来创建这个文件 - Static.scala

package controllers 

object Static extends AssetsBuilder 

然后在你的路线,你可以定义静态终结

GET /file controllers.Static.at(path="/public/html", "file.html") 

完成。现在,在/public/html/file.html文件将送达断localhost:9000/file

如果更换上面一个更通用的硬代码:

GET /*file controllers.Static.at(path="/public/html", *file + ".html") 

然后/foo将成为/public/html/foo.html/bar将成为/public/html/bar.html

+1

测试和工作! +1 – Jus12 2014-10-05 16:43:47

+1

这是一个不错的诀窍! +1 – 2014-11-12 18:22:59

5

你的第三次尝试几乎是正确的。 而不是

GET /file controllers.Assets.at(path="/public/html", file="file.html") 

做这样的

GET /file controllers.Assets.at(path="/public", file="html/file.html") 

我之前得到了同样的问题。我的路由文件看起来像这样。

# Application 
GET/  controllers.Assets.at(path="/public/html", file="static.html") 
GET /exmpl controllers.Examples.index 

# Resources 
GET /assets/*file controllers.Assets.at(path="/public", file) 

而且我有反向路由低于内部意见(examples.scala.html

@routes.Assets.at("imagefolder") #try to generate path to /public/imagefolder. 

当我打开http://localhost:9000/exmpl,这个错误出现了。

not enough arguments for method at: (path: String, file: String)play.api.mvc.Call. Unspecified value parameter file. 

要解决这个问题,我只是改变这条路线

GET/ controllers.Assets.at(path="/public/html", file="static.html") 

这个

GET/ controllers.Assets.at(path="/public", file="html/static.html") 

该解决方案对我的作品。我希望它也适用于你和其他人。

1

在jar文件中播放java包公用文件夹,如果您想要提供解析为服务器中绝对文件位置的静态文件,则会出现问题。正确的做法是拥有自己的控制器并使用它来提供静态文件。

对于离,以服务文件“mystatic.html”,也就是在

<your playhome> 
    |-----<myfolder> 
     |------mystatic.html 

你会配置你的路由文件,这条路线。

GET /mystatic   mypackage.mycontrollers.Static.getFile(path="/myfolder/mystatic.html") 

和您的控制器将实施如下。

package mypackage.mycontroller; 

import java.io.File; 
import com.google.inject.Inject; 
import com.google.inject.Provider; 

import play.Application; 
import play.mvc.Controller; 
import play.mvc.Result; 
import play.mvc.Results; 

public class Static extends Controller { 

    @Inject 
    Provider<Application> app; 

    public Result getFile(String path){ 
     File file = app.get().getFile(path); 
     if(file.exists()){ 
      return ok(file);    
     }else{ 
      return Results.notFound(); 
     } 
    } 
}