在Play中路由到静态文件!2.0

Nit*_*mer 32 routes static-files playframework playframework-2.0

我正在尝试建立一个特定静态文件的路由,但我正在尝试的所有内容都以错误结束.

我做了3次不同的尝试:

1.

GET /file   staticFile:/public/html/file.html
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

Compilation error
string matching regex `\z' expected but `:' found
Run Code Online (Sandbox Code Playgroud)

2.

GET /file   controllers.Assets.at(path="/public/html", "file.html")
Run Code Online (Sandbox Code Playgroud)

我得到的错误:

Compilation error
Identifier expected
Run Code Online (Sandbox Code Playgroud)

3.

GET /file   controllers.Assets.at(path="/public/html", file="file.html")
Run Code Online (Sandbox Code Playgroud)

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

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

关于第3个错误的奇怪部分是它被抛出在以下行的不同文件(app/views/main.scala.html)中:

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

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

谢谢.

Jam*_*mil 18

IIRC,改变

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

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

我说的是你的第三次尝试

另外,请注意额外/

编辑

GET /assets/main.css    controllers.Assets.at(path="/public", file="/stylesheets/main.css")
Run Code Online (Sandbox Code Playgroud)

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

  • 这只是解决了另一个错误,但你改变的不是问题,当我注释掉这条路由线一切正常(除了文件没有提供).第三次尝试中的错误是主模板文件,这正是我从_play new app-name_命令得到的,我没有改变它,而且正如我所写的,它没有那个路由就可以正常工作. (2认同)

Nic*_*ick 13

我不完全确定这是否正确,但这是我们用来映射包含我们的图像,javascripts等的公共文件夹.

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


小智 13

据我所知,这项能力仍然没有增加.但是如果有人需要回答,作为一个选项,可以为这些目的创建帮助控制器:

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
  }

}
Run Code Online (Sandbox Code Playgroud)

然后在路由配置中

GET     /               controllers.StaticFile.html(file = "public/index.html")
Run Code Online (Sandbox Code Playgroud)


Mic*_*len 10

最简洁的解决方案是创建自己的AssetsBuilder来构建静态页面.

在您的控制器包中创建此文件 - Static.scala

package controllers

object Static extends AssetsBuilder
Run Code Online (Sandbox Code Playgroud)

然后在您的路线中,您可以定义静态端点

GET /file   controllers.Static.at(path="/public/html", "file.html")
Run Code Online (Sandbox Code Playgroud)

完成.现在文件/public/html/file.html将被提供localhost:9000/file

如果您将上面的硬代码替换为更通用的:

GET /*file   controllers.Static.at(path="/public/html", *file + ".html")
Run Code Online (Sandbox Code Playgroud)

然后/foo将服务/public/html/foo.html,/bar将服务/public/html/bar.html


小智 5

你的第三次尝试几乎是对的.代替

GET /file   controllers.Assets.at(path="/public/html", file="file.html")
Run Code Online (Sandbox Code Playgroud)

像这样做

GET /file   controllers.Assets.at(path="/public", file="html/file.html")
Run Code Online (Sandbox Code Playgroud)

我以前遇到过同样的问题.我的路线文件看起来像这样.

# 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)
Run Code Online (Sandbox Code Playgroud)

我在视图内部有反向路线(examples.scala.html)

@routes.Assets.at("imagefolder")   #try to generate path to /public/imagefolder.
Run Code Online (Sandbox Code Playgroud)

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

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

为了解决这个问题,我刚刚改变了这条路线

GET /     controllers.Assets.at(path="/public/html", file="static.html")
Run Code Online (Sandbox Code Playgroud)

对此

GET /     controllers.Assets.at(path="/public", file="html/static.html")
Run Code Online (Sandbox Code Playgroud)

这个解决方案对我有用.我希望它对你和其他人都有用.