playframework 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
삼.
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")">
이 방법들은 모두 에서 공식 문서 및 / 또는 스레드에서 찾을 수 있습니다. 내가 여기서 무엇을 놓치고 있니?
감사.
이것에 대한 솔루션은 정적 페이지를 구축 할 자신 만의 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
등을 게재합니다.
나는 똑같은 문제가있다. @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))
나는 이것이 대답이 아니라는 것을 안다. 그러나 아마 누군가가 하나와 파이프로 연결될 수있게 해줄 것이다.
나는 같은 문제에 부딪쳤다. 두 번째 controllers.Assets.at
를 다음과 같은 경로에 추가했을 때
GET /assets/*file controllers.Assets.at(path="/public", file)
GET /assets2/*file controllers.Assets.at(path="/public2", file)
@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")"
유감스럽게도 경로에 자산 라인이 하나만있는 경우 컴파일 오류를 피하기 위해 단일 매개 변수 폼으로 변경해야합니다. 나 한테 버그 같아.