1

我正在创建一个游戏!2.1.1 应用程序,我使用 Play2War 打包成一个 war 文件。这需要我application.context=/theApp/在 application.conf 文件中添加一个上下文 , 。

我将 WAR 文件部署到了一个 tomcat7 服务器,这导致了 url localhost:8080/theApp/。

CSS/JS 文件在 url 为 example 时加载http://localhost:8080/theApp/thisseemstowork,但一旦 url 为 则http://localhost:8080/theApp/thisoughttowork/180没有任何 CSS/JS 文件被加载。我只是得到

GET http://localhost:8080/theApp/thisoughttowork/public/javascripts/vendor/bootstrap.min.js 404 (Not Found) 

这就是我在视图中链接到 js 文件的方式:

    <script src="public/javascripts/vendor/bootstrap.min.js"></script>

这是在我的路线文件中

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

有人知道我做错了什么吗?如果您需要更多信息,请告诉我。

4

1 回答 1

3

您需要使用资产控制器和反向路由。

<script src="@routes.Assets.at("javascripts/vendor/bootstrap.min.js")"></script>

无论您在应用程序中的哪个位置,这都会生成正确的路径。正如您从发布的路由文件片段中看到的那样,路径是相对于/public文件夹的,因此这适用于您放入其中的任何样式表、图像等。

<link rel="stylesheet" href="@Assets.at("stylesheets/bootstrap.min.css")">
<link rel="stylesheet" href="@Assets.at("other/folder/styles.css")">

这是 Play 的文档:

http://www.playframework.com/documentation/2.1.1/Assets

于 2013-04-29T22:20:00.263 回答