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