首页 文章

groovy - findAll只获得一个值

提问于
浏览
0

我正在努力寻找带有groovy的findAll的例子 . 我有一个非常简单的代码片段,它获取节点的属性并输出它的值 . 除非我在循环浏览一系列属性时才获得最后一个值 . 这里有什么我做错了,这看起来很简单 .

JcrUtils.getChildNodes("footer").findAll{ 
 selectFooterLabel = it.hasProperty("footerLabel") ? it.getProperty("footerLabel").getString() : ""
}

在我的jsp中,我只是打印属性:

<%=selectFooterLabel%>

谢谢您的帮助!

1 回答

  • 2

    findAll 返回 List ,其中包含原始列表中闭包返回Groovy-true值的所有项(布尔值为true,非空字符串/ map / collection,非null为null) . 看起来你可能想要 collect

    def footerLabels = JcrUtils.getChildNodes("footer").collect{ 
     it.hasProperty("footerLabel") ? it.getProperty("footerLabel").getString() : ""
    }
    

    它将为您提供闭包返回的值的列表 . 如果您只想要那些非空的子集,您可以使用 findAll() 而不使用闭包参数,这将为您提供列表中的值的子集,这些值本身就是Groovy-true

    def footerLabels = JcrUtils.getChildNodes("footer").collect{ 
     it.hasProperty("footerLabel") ? it.getProperty("footerLabel").getString() : ""
    }.findAll()
    

相关问题