[英]Route to static file in 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.
关于第3个错误的奇怪部分是它被抛出在以下行的不同文件(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")">
至
<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
我不完全确定这是否正确,但这是我们用来映射包含我们的图像,javascripts等的公共文件夹。
# 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")
最简洁的解决方案是创建自己的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
等。
你的第三次尝试几乎是对的。 代替
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")
这个解决方案对我有用。 我希望它对你和其他人都有用。
我有完全相同的问题。 我遵循@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))
我知道这不是一个答案,但也许它会让某人用一个人来管。
当我尝试配置一些额外的CSS时,我遇到了同样的问题。 它在“routes”文件中使用了这种语法
GET /css/*file controllers.Assets.at(path="/public/css", file)
我遇到了同样的问题。 当我添加第二个controllers.Assets.at
到路由之类的
GET /assets/*file controllers.Assets.at(path="/public", file)
GET /assets2/*file controllers.Assets.at(path="/public2", file)
@routes.Assets.at
的默认@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")"
不幸的是,如果您回到路由中只有一个资产行,则必须更改为单参数表单以避免编译错误。 对我来说似乎是一个错误。
在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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.