Route to static file in Play! 2.0

2020-05-13 16:09发布

I'm trying to make a route to a specific static file but everything I'm trying ends with an error.

I've made 3 different attempts:

1.

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

The error I get:

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

2.

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

The error I get:

Compilation error
Identifier expected

3.

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

The error I get: (and this is the weirdest)

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

The weird part about the 3rd error is that it's thrown in a different file (app/views/main.scala.html) on the following line:

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

All of these methods were found in the official documentation and/or threads here on stackoverflow. What am I missing here?

Thanks.

9条回答
成全新的幸福
2楼-- · 2020-05-13 16:14

The solution to this that is cleanest is to create your own AssetsBuilder that will build your static page.

Create this file in your controllers package - Static.scala

package controllers

object Static extends AssetsBuilder

Then in your routes you can define your static endpoint

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

Done. Now the file at /public/html/file.html will be served off of localhost:9000/file

If you replace the hard code above with a more generic:

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

then /foo will serve /public/html/foo.html, /bar will serve /public/html/bar.html etc.

查看更多
趁早两清
3楼-- · 2020-05-13 16:16

I have the exact same issue. I followed the advice from @Jamil and managed to get this working for the static file (in my case a favicon) and managed to get the templates to compile but get a new error at runtime when trying to use the view. Relevant code below,

Change to the route (this route now resolves correctly)

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

Change to the view (compiles)

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

New error (only at runtime)

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

I know this isnt an answer but perhaps it will allow someone to pipe up with one.

查看更多
Viruses.
4楼-- · 2020-05-13 16:17

IIRC, change

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

To

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

I am talking about your third attempt

Also, watch out for extra /

EDIT

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

Assuming your resource is at /public/stylesheets/main.css

查看更多
叼着烟拽天下
5楼-- · 2020-05-13 16:29

I am not totally sure if this is correct, but this is what we are using to map a public folder containing our images, javascripts... etc..

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)
查看更多
在下西门庆
6楼-- · 2020-05-13 16:29

This ability still haven't been added, as I know. But if someone needs answer, as an option, helper controller can be created for these purpose:

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
  }

}

and then in routes config

GET     /               controllers.StaticFile.html(file = "public/index.html")
查看更多
劳资没心,怎么记你
7楼-- · 2020-05-13 16:30

I was experiencing the same issue while I was trying to configure some additional css. It worked with this syntax in the "routes" file

  GET   /css/*file                      controllers.Assets.at(path="/public/css", file)
查看更多
登录 后发表回答