# Grails 1.1 GSP 搜索和过滤

**URL:** <https://www.isharkfly.com/t/grails-1-1-gsp/6690>\
**Category:** 程序语言\
**Created:** [November 4, 2012, 5:25pm UTC](https://www.isharkfly.com/t/grails-1-1-gsp/6690 "2012-11-04T17:25:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [November 4, 2012, 5:25pm UTC](https://www.isharkfly.com/t/grails-1-1-gsp/6690/1 "2012-11-04T17:25:07Z")

</div>

假如你拥有对象集合，你经常需要使用一些方法来排序和过滤他们。

GSP 支持 findAll 和grep 来做这些工作:[code]Stephen King’s Books:  
\<g:findAll in=“${books}” expr=“it.author == ‘Stephen King’”\>

Title: ${it.title}

[/code]expr属性包含了一个Groovy表达式，它可以被当作一个过滤器来使用。

谈到过滤器，grep标签通过类来完成与过滤器类似的工作:[code]\<g:grep in=“${books}” filter=“NonFictionBooks.class”\>

Title: ${it.title}

[/code]或者使用一个正则表达式:[code]

Title: ${it}

[/code]上面的示例同样有趣，因为它使用了GPath.Groovy的GPath等同与XPath语言。

实际上books集合是books集合的实体。

不过，假设每个books拥有一个title,你可以使用表达式books.title来获取Book titles的list!
