Playframework 路由到播放中的静态文件!2

Playframework 路由到播放中的静态文件!2,playframework,routes,playframework-2.0,static-files,Playframework,Routes,Playframework 2.0,Static Files,我试图路由到一个特定的静态文件,但我尝试的每件事都以一个错误结束 我做了3次不同的尝试: 一, 我得到的错误是: Compilation error string matching regex `\z' expected but `:' found Compilation error Identifier expected 二, 我得到的错误是: Compilation error string matching regex `\z' expected but `:' found Comp

我试图路由到一个特定的静态文件,但我尝试的每件事都以一个错误结束

我做了3次不同的尝试:

一,

我得到的错误是:

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

我得到的错误是:

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

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

第三个错误的奇怪之处在于,它被抛出到下一行的另一个文件(app/views/main.scala.html)中:

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

所有这些方法都可以在stackoverflow的官方文档和/或线程中找到。 我错过了什么

谢谢。

IIRC,换衣服

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

假设您的资源位于/public/stylesheets/main.css

我在尝试配置其他css时遇到了相同的问题。它在“routes”文件中使用了这种语法


我有完全相同的问题。我遵照@Jamil的建议,设法使静态文件(在我的例子中是favicon)能够工作,并设法编译模板,但在尝试使用视图时,在运行时出现了一个新错误。相关代码如下:

更改路由(此路由现在可以正确解析)

更改视图(编译)


我知道这不是一个答案,但也许它会让人用管道说出答案。

我不完全确定这是否正确,但这是我们用来映射包含图像、Java脚本的公用文件夹的内容。。。等等

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

据我所知,这个技能还没有被添加。但如果有人需要答案,作为一个选项,可以为以下目的创建帮助器控制器:

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
  }

}
然后在路由配置中

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

我遇到了同样的问题。当我添加第二个
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")"

不幸的是,如果返回到routes中只有一个资产行,则必须更改为单参数表单以避免编译错误。对我来说似乎是个bug。

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

在控制器包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
等。

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

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

当我打开
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")

这个解决方案对我来说很有效。我希望它也适用于您和其他人。

在jar文件中播放java packages公用文件夹,如果您希望提供解析为服务器中绝对文件位置的静态文件,这将是一个问题。正确的方法是拥有自己的控制器,并使用它为静态文件提供服务

例如,提供一个文件“mystatic.html”,该文件位于

<your playhome>
    |-----<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();
        }
    }   
}
package mypackage.mycontroller;
导入java.io.File;
导入com.google.inject.inject;
导入com.google.inject.Provider;
导入play.Application;
导入play.mvc.Controller;
导入play.mvc.Result;
导入play.mvc.Results;
公共类静态扩展控制器{
@注入
提供商应用程序;
公共结果getFile(字符串路径){
File File=app.get().getFile(路径);
if(file.exists()){
返回ok(文件);
}否则{
返回结果。notFound();
}
}   
}

这只是解决了另一个错误,但您所做的更改并不是问题所在,当我注释掉这条路由行时,所有工作都很好(除了没有提供文件)。第三次尝试中的错误是主模板文件的错误,这正是我从play new app name命令中得到的,我没有更改它,正如我所写的,在没有该路由的情况下,它工作得很好。我认为第三次错误发生在默认模板中提到第一个公共路由实例的情况下。我有完全相同的问题。我希望你/我尝试添加的静态链接会弄乱反向路由。什么?两年了,没有公认的答案。这个问题解决了吗?@Jus12对不起,只是我没有继续使用play(不是我自己选择的,很不幸),我不知道哪个答案是正确的。当时得票最多的那个并没有解决这个问题(正如我的评论所解释的)。如果有人让我知道任何答案是否正确,我会很高兴地将其标记为已接受。@NitzanTomer为我工作。@Jus12由于我不确定如何进行此操作,我在元站点中寻求建议,看起来应该保持原样:感谢您的回复,我目前还没有办法尝试此操作,我希望能回到这里并报告。正确的路线是:“get/favicon.ico controllers.Assets.at(path=“/public”,file=“images/favicon.ico”)”谢谢你的回复,我到目前为止没有办法尝试,我希望能回到这里并报告。如果我将“public”文件夹重命名为“Assets”,这似乎不起作用(使用
path=“/assets”
,…)。知道为什么吗?和上面一样,它有效,但我们似乎不知道为什么
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)
@routes.Assets.at("imagefolder")   #try to generate path to /public/imagefolder.
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")
<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();
        }
    }   
}