Play Framework @routes.Assets.at 编译错误

37

我正在使用Play 2.4.0,一直试图按照主页上的教程进行操作:https://playframework.com/,该教程适用于Play 2.3。在解决了Ebean ORM从版本2.3到2.4的更改所导致的一些问题后,我现在被以下错误困扰:

Compilation error

value at is not a member of controllers.ReverseAssets

我的index.scala.html

@(message: String)

@main("Welcome to Play") {

    <script type='text/javascript' src="@routes.Assets.at("javascripts/index.js")"></script>

    <form action="@routes.Application.addPerson()" method="post">
        <input type="text" name="name" />
        <button>Add Person</button>
    </form>

    <ul id="persons">
    </ul>
}

我的routes文件如下:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET         /                    controllers.Application.index()

POST        /person              controllers.Application.addPerson()

GET         /persons             controllers.Application.getPersons()

# Map static resources from the /public folder to the /assets URL path
GET         /assets/*file        controllers.Assets.versioned(path="/public", file: Asset)

我已经成功地使用Play 2.3.9实现了相同的示例。

在2.4.0的文档中,我没有看到任何有关使用公共资产的不同之处:https://www.playframework.com/documentation/2.4.0/Assets

因此...非常感谢任何帮助。


1
你尝试过运行sbt clean吗? - Roman
1
我尝试了 activator cleanactivator clean-files,但是我得到了相同的错误。 - Daniel Romero
3
据我记忆,当我迁移到Play 2.4时,出现了类似的错误。我的资源路由看起来像这样:GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)。请注意使用了versioned而不是at。也许这会有所帮助。如果没有,您可以发布您的路由配置可能更有帮助。 - Roman
5
忘了提到,您还需要在index.scala.html中将@routes.Assets.at("javascripts/index.js")更改为@routes.Assets.versioned("javascripts/index.js") - Roman
2
@Roman请创建答案,以便它可以被接受和投票支持。 - biesior
显示剩余2条评论
1个回答

70

总结解决方案:Play框架提供了两种不同的方式来服务于您的资源,传统的和新的指纹方法,后者是通过sbt-web引入的。无论哪种情况,请确保在您的视图文件中使用正确的调用:

指纹资源

这是在Play框架中推荐的服务资源的方式。指纹资源采用了一种积极的缓存策略。您可以在此处了解更多信息:https://playframework.com/documentation/2.4.x/Assets

路由配置:

GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)

确保file的类型被指定为Asset

在视图中调用:

@routes.Assets.versioned("an_asset")


旧式资产

这基本上是在引入sbt-web之前使用的方法。

路由配置:

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

呼入视图:

@routes.Assets.at("an_asset")

3
IntelliJ错误地将Assets.versioned()报告为已弃用,目前尚未修复,详见https://youtrack.jetbrains.com/issue/SCL-8812。 - Stefan L

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接