【问题标题】:Why doesn't .collect() work in the following GString?为什么 .collect() 在以下 GString 中不起作用?
【发布时间】:2009-11-13 13:23:36
【问题描述】:

这在 GSP 页面中按预期工作:

<td>${Foo.findAllByBar(bar)}</td>

但是当添加一个收集语句时,代码会中断..

<td>${Foo.findAllByBar(bar).collect { it.name }}</td>

Error 500: Could not parse script [...gsp]: startup failed,
     ...: 129: expecting '}', found ')'
     @ line 129, column 196. 1 error`.

我的印象是任何有效的 Groovy 代码都可以放在 GString ${ ... } 中并被正确评估/扩展。我错过了什么?

【问题讨论】:

    标签: grails groovy gsp gstring


    【解决方案1】:

    或者,您可以使用spread operator

    <td>${Foo.findAllByBar(bar)*.name}</td>
    

    【讨论】:

      【解决方案2】:

      GSP 解析器不喜欢 ${...} 块中的 }。试试这个:

      <%= Foo.findAllByBar(bar).collect { it.name } %>
      

      【讨论】:

      • 谢谢!使用 解决了这个问题。这是一个错误还是设计使然?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 2015-05-26
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多