struts 标签学习笔记

作用:在一个循环中遍历树组、Collection、Enumeration、Iterator、或 Map 中的所有元素。
   <%
        HashMap months = new HashMap();
        months.put("Jan.", "January");
        months.put("Feb.", "February");
        months.put("Mar.", "March");
        request.setAttribute("months", months);
   %>
    <logic:iterate id="element" indexId="ind" name="months">
        <bean:write name="ind"/>. //输出序号
        <bean:write name="element" property="key"/>: 输出key键
        <bean:write name="element" property="value"/><BR>  //输出value值
    </logic:iterate>
    ============================================================
   在map中存储 集合对象,
   =============================================================
    <%
      HashMap h = new HashMap();
      String vegetables[] = {"pepper", "cucumber"};
      String fruits[] = {"apple","orange","banana","cherry","watermelon"};
      String flowers[] = {"chrysanthemum","rose"};
      String trees[]={"willow"};
      h.put("Vegetables", vegetables);
      h.put("Fruits", fruits);
      h.put("Flowers", flowers);
      h.put("Trees",trees);
      request.setAttribute("catalog", h);
     %>
     ===========================================================
     以上代码定义一个名为“catalog”的HashMap,存放在request范围内。他的每个
     元素的value 为字符串数组。采用嵌套的<logic:iterator>标签来遍历。
     ===========================================================
<logic:iterate id="element" indexId="ind" name="catalog">
      <bean:write name="ind"/>.
      <bean:write name="element" property="key"/><BR>
            <logic:iterate id="elementValue" name="element" property="value"                          length="3" offset="1">
             -----    <bean:write name="elementValue"/><BR>
            </logic:iterate>
</logic:iterate>

================================================================
================================================================
遍历 Vector

<%
 Vector animals=new Vector();
 animals.addElement("Dog");
 animals.addElement("Cat");
 animals.addElement("Bird");
 animals.addElement("Chick");
 request.setAttribute("Animals", animals);
%>
<logic:iterate id="element" name="Animals">
   <bean:write name="element"/><BR>
</logic:iterate>

<logic:iterate id="element" indexId="index" name="Animals" offset="1" length="2">
   <bean:write name="index"/>.<bean:write name="element"/><BR>
</logic:iterate>
===============================================================
遍历 Collection
===============================================================
<logic:iterate id="header" collection="<%= request.getHeaderNames() %>">
   <bean:write name="header"/><BR>
</logic:iterate>

你可能感兴趣的:(apple,bean,struts)