【问题标题】:Use multiple Assets route in Play Framework 2在 Play Framework 2 中使用多个 Assets 路由
【发布时间】:2014-08-09 10:32:49
【问题描述】:

我的申请中有这条路线:

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

我在 scala 模板中使用了反向路由,它工作正常:

<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/bootstrap.css")"> 

但是当我为图片添加这条路线时:

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

我在正常工作的 scala 模板中遇到以下错误:

not enough arguments for method at: (path: String, file: String)play.api.mvc.Call.
Unspecified value parameter file.

我不能在 Play Framework 2 或其他的路由中使用 controllers.Assets.at 两次?

【问题讨论】:

    标签: playframework routes playframework-2.0 assets


    【解决方案1】:

    正如documentation 中所述,我相信您可以为您的资产添加 1 条或更多条路线。

    这是我认为您错过的重要部分:

    此操作将查找文件并提供它(如果存在)。

    请注意,如果您在“公共”之外定义资产映射,则需要 告诉 sbt,例如如果你愿意:

    GET  /assets/*file               Assets.at("public", file)
    GET  /liabilities/*file          Assets.at("foo", file)
    

    你应该把它添加到 Build.scala:

    playAssetsDirectories <+= baseDirectory / "foo"
    

    这用于使用反向路由:

    但是,如果您为 Assets.at 操作定义两个映射,例如 这个:

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

    那么你需要在使用反向时指定这两个参数 路由器:

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

    编辑:

    尝试将您的 CSS 链接更改为:

    <link rel="stylesheet" media="screen" href="@routes.Assets.at("/public","stylesheets/bootstrap.css")"> 
    

    【讨论】:

    • 你试过这样称呼你的图片src="@routes.Assets.at("/public/images", "filename.png")"吗?如果你这样做了,那么错误一定在其他地方..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 2014-04-03
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2015-12-09
    相关资源
    最近更新 更多