In Playframework2, how to serve multiple asset dir

2019-05-08 06:23发布

问题:

There are two asset directory, one is /ui/dist and the other is /public. I tried to write the routes like this:

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

But it will throws an error while compiling:

[error] Unspecified value parameter file.
[error]         <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">

Does anyone have ideas about how to solve this...

回答1:

I found the key is to pass in another parameter to routes.Assets.at, the following is from the playframework Asset document

GET  /javascripts/*file        Assets.at("public/javascripts", file)
GET  /images/*file             Assets.at("public/images", file)

Then you will need to specify both parameters when using the reverse router:

<script src="@routes.Assets.at("public/javascripts", "jquery.js")"></script>
<image src="@routes.Assets.at("public/images", "logo.png")">


回答2:

Try creating a separate router object for each one:

package controllers

object PublicAssets extends controllers.AssetsBuilder
object UiDistAssets extends controllers.AssetsBuilder

and then in your routes:

GET     /assets/*file   controllers.PublicAssets.at(path="/public", file)
GET     /ui/*file       controllers.UiDistAssets.at(path="/ui/dist", file)

I think what's happening in your attempt is that the reverse router is generating an object for your public routes, and then another one with the same name for the ui routes which overwrites the first (if anyone knows more precisely, please correct me.) The solution should be to name your asset classes differently, so the generated reverse routes likewise have different names (or, alternately, call them the same thing but put them in different packages.)