logic:iterate

來源:互聯網
上載者:User

 logic:iterate標籤用來迭代集合,您可以使用如下方式來為這個標籤指定其要疊代的集合:

使用一個運行時運算式,這個運算式的值是一個集合。
用name屬性引用一個JSP Bean,這個JSP Bean本身就是一個集合。
用name屬性引用一個JSP Bean,這個JSP Bean的一個屬性是一個集合,這時可以聯合使用property來指定這個集合。
上面所提到的集合可以是:

物件類型或原子類型的數組(Array)。
java.util.Collection的實現,包括ArrayList,Vector。
java.util.Enumeration的實現。
java.util.Iterator的實現。
java.util.Map的實現,包括HashMap,Hashtable和TreeMap。
如果您疊代的集合中含有null的值,這時需要採取一定的措施,因為這時logic:iterate不會在page範圍中建立對象。一般是使用<logic:present>標籤或<logic:notPresent>標籤來判斷一下。

下面是logic:iterate疊代ArrayList的樣本的對象參考關聯性和部分代碼:

圖示 3. logic:iterate中對象的參考關聯性
 
圖中的persons列表是在ListAction中填充的,在這裡只是簡單的加入了三個Person對象,在實際的應用中這些資料應該取自資料庫。具體的代碼如下:

public ActionForward execute(ActionMapping mapping,ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
  ListForm listForm = (ListForm) form;
 
  List<Person> persons = new ArrayList<Person>();
 
  Person person1 = new Person();
  person1.setId("00001");
  person1.setName("趙辰");
 
  Person person2 = new Person();
  person2.setId("00002");
  person2.setName("李為芳");
 
  Person person3 = new Person();
  person3.setId("00003");
  person3.setName("王微");
 
  persons.add(person1);
  persons.add(person2);
  persons.add(person3);
 
  listForm.setPersons(persons);
 
  return mapping.findForward("success");
}
標籤輸出的結果為:

00001-->趙辰
00002-->李為芳
00003-->王微

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/AWUSOFT/archive/2008/05/16/2452874.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.