Play Framework @routes.Assets.at Compilation Error

2020-05-19 03:03发布

问题:

I'm using Play 2.4.0 and I've been trying to follow the tutorial from the main page: https://playframework.com/ which is for Play 2.3 and after solving a couple of issues regarding changes in the Ebean ORM from version 2.3 to 2.4, I'm stuck with the following error:

Compilation error

value at is not a member of controllers.ReverseAssets

My 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>
}

And my routes file:

# 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)

I have this same example working ok with Play 2.3.9

And I can't see anything different about working with public assets in the docs for the 2.4.0: https://www.playframework.com/documentation/2.4.0/Assets

So... any help would be appreciated.

回答1:

Alright, to sum up the solution: Play lets you serve your assets in two different ways. The old fashioned and the new fingerprinted method introduced with sbt-web. In either case make sure you use right call in your view files:

Fingerprinted assets

This is the recommended way to serve assets in play. Fingerprinted assets make use of an aggressive caching strategy. You can read more about this topic here: https://playframework.com/documentation/2.4.x/Assets

routes config:

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

Make sure the type of file is indicated as Asset

call in views:

@routes.Assets.versioned("an_asset")


Old fashioned assets

This is basically the method used before the introduction of sbt-web.

routes config:

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

call in views:

@routes.Assets.at("an_asset")