32

我正在尝试创建到特定静态文件的路由,但我尝试的所有内容都以错误结束。

我做了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")">

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

谢谢。

4

9 回答 9

19

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

于 2012-03-21T20:27:13.610 回答
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
  }

}

然后在路由配置中

GET     /               controllers.StaticFile.html(file = "public/index.html")
于 2012-12-26T14:52:47.670 回答
13

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

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)
于 2012-08-02T02:21:02.880 回答
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")

then /foowill serve /public/html/foo.html, /barwill serve/public/html/bar.html

于 2013-08-14T15:36:34.300 回答
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")

这个解决方案对我有用。我希望它也适用于你和其他人。

于 2013-09-01T15:31:34.313 回答
4

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

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

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

我知道这不是一个答案,但也许它会让某人用一个管道来解决这个问题。

于 2012-08-01T23:13:18.423 回答
3

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

  GET   /css/*file                      controllers.Assets.at(path="/public/css", file)
于 2012-05-27T21:41:23.033 回答
1

我遇到了同样的问题。当我controllers.Assets.at在路线上增加一秒钟时

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

@routes.Assets.atmain.scala.html中的默认调用编译失败

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

不幸的是,如果您回到路由中只有一个资产行,您将不得不更改为单参数形式以避免编译错误。对我来说似乎是一个错误。

于 2013-05-23T23:25:09.353 回答
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();
        }
    }   
}
于 2017-02-09T14:55:02.533 回答