2015-11-03 33 views
0

我把main.scala.html放在資產中。我想打開它而不設置路由,因爲它是靜態的。如何打開一個靜態的.html頁面,而無需在Play框架中設置路線?

在controllers.Application,我想這樣的

public static Result index() { 
    //return ok(index.render("Your new application is ready.")); 
    return redirect("assets/main/main.scala.html"); 

} 

而在航線頁面,我想這樣的

GET/controllers.Application.index

GET/*文件控制器。 Assets.at(path =「/ assets」,文件)

如何修復我的代碼以打開http://localhost:9000/assets/main/main.scala.html

或者我應該嘗試將html文件放在公用文件夾中?

+0

我不會找到一種不同的方式爲用戶提供靜態內容。我建議堅持通過在控制器中呈現模板和文件來提供內容的常用方式,併爲其添加路由。它更簡單,更優雅。 – kevto

+0

感謝您的回覆。我也喜歡通常的方式,但是我的老闆想要訓練我,讓我把html文件放在資產中,因爲我已經用平常的方式製作了一個。 –

回答

1

1)如果它是靜態的,你不應該把它命名爲main。 scala .html,但只是main.html。 (不是強制性的,但只是要清楚是什麼)

2)正如註釋中所寫,您應該通過routes.conf和Controller維護路由。但是,您可以將文件加載爲資源而不使用模板。

GET   /main    controllers.Application.publicMain 

public static Result publicMain{ 
return ok(Application.class.getResourceAsStream("/assets/main/main.html")).as("text/html"); 
    } 
0

儘管不推薦,但如果你真的想這樣做,你可以像@mst建議的那樣做,或者讀取文件並將其解析爲html。 例如:

val html = 
     """ 
     <html> 
     <body> 
     <p> Hello </p> 
     </body> 
     </html> 
     """ 
    Ok(scala.xml.Unparsed(html)).as("text/html") 
    }