list.{?#conditional expression>value}
如在一個集合中包含多個學生物件,獲取這個集合中學生年齡大於 10 的所有學生,就是選擇操作。
public class OgnlAction extends ActionSupport{ //定義List型別books變數 private List<Book>books; public List<Book>getBooks(){ return books; } public String execute(){ //初始化books物件 books = new LinkedList<Book>(); //應用add()方法為List型別books變數賦值 books.add(new Book("978-0735619678","程式設計寶典",50.99)); books.add(new Book("978-0596007867","典型模組大全",35.96)); books.add(new Book("978-0201633610","開發實戰寶典",50.19)); books.add(new Book("978-0596527341","範例大全",25.19)); books.add(new Book("978-0735605350","技術開發大全",50.19)); return SUCCESS; } }
<body> <h3>用於過濾集合</h3> <p>書的價格超過50美元的是</p> <ul> <s:iterator value="books.{?#this.price>50}"> <li><s:property value="title"/>-$<s:property value="price"/></li> </s:iterator> </ul> <p>"範例大全"的價格是:<s:property value="books.{?#this.title=='範例大全'}.{price} [0]"/></p> </body>}