【问题标题】:Java Play 2 - Function as a parameter in scalaJava Play 2 - 作为scala中的参数的函数
【发布时间】:2012-08-23 19:34:29
【问题描述】:

我在 scala 中编写可重用代码时遇到问题。

如果我有类似的东西

@helper.form(action = routes.Guides.addComment(category,title)) {

有没有办法用变量替换它?

伪代码

@(func : Function)
@helper.form(action = func) {

编辑:

哦....现在有点明显了。函数本身应该返回一个字符串,所以我想我可以这样说

@(func :String)
..

.

return ok (form.render(routes.Guides.test()))

现在测试

【问题讨论】:

    标签: java templates scala playframework-2.0


    【解决方案1】:

    我可以建议一个替代方案吗?直接使用Call

    @(route: Call)
    
    @helper.form(action = route) {
      ...
    }
    

    在 Scala 中,您甚至可以只传递部分路由并从控制器填充其余部分(在使用分页时非常有用)。

    【讨论】:

    • 你能解释一下如何使用它进行分页吗?
    • 你能单独问一个问题吗?
    【解决方案2】:

    想通了。

    routes.Guides.test().url
    

    你得到了url,然后你可以把它用作参数

    例如

    @guidesComment((routes.Guides.addComment(ug.category,ug.title)).url)
    

    guidesComment 看起来像这样

    @(func: String)
    

    那就这样用吧

    <form action="@func" method="POST">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 2012-12-02
      • 2018-06-28
      • 1970-01-01
      • 2021-08-12
      相关资源
      最近更新 更多