2009-07-23

struts logic:iterate

 

   在邏輯標記庫中定義了<logic:iterate>標記,它能夠根據特定集合中元素的數目對標記體的內容進行重複的檢查。集合的類型可以是java.util.Iterator,java.util.Collection,java.util.Map

或是一個陣列,也可以用name屬性定義一個bean,並且使用property屬性指定要迴圈的bean的屬性(這個屬性返回集合)類型。

 

下面請看struts1.2.4中自帶的例子:

 

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>

<%@ taglib uri="/tags/struts-logic" prefix="logic" %>

<html>

  <head>

    <title>測試struts-logic Iterate 標記</title>

  </head>

  <body bgcolor="white">     

 

    <%

      {

        java.util.ArrayList list = new java.util.ArrayList();

        list.add("First");

        list.add("Second");

        list.add("Third");

        list.add("Fourth");

        list.add("Fifth");

        pageContext.setAttribute("list", list, PageContext.PAGE_SCOPE);

 

        int intArray[] = new int[]

         { 0, 10, 20, 30, 40 };

        pageContext.setAttribute("intArray", intArray, PageContext.PAGE_SCOPE);

      }

    %>

    <div align="center">

      <h1>測試struts-logic Iterate標記</h1>

    </div>

    <jsp:useBean id="bean" scope="page" class="org.apache.struts.webapp.exercise.TestBean" />

    <jsp:useBean id="list" scope="page" class="java.util.ArrayList" />

 

    <h3>測試1 - 從頭到尾疊代(迴圈)一個字串陣列[0..4]</h3>

 

    <ul>

      <!--name屬性指定一個bean,property屬性指定要迴圈的bean的屬性,id集合中當前元素的控制碼,index1保存當前索引值-->

      <logic:iterate id="element" name="bean" property="stringArray" indexId="index1">

        <li>

        <em>

          <bean:write name="element" />

        </em> [

        <bean:write name="index1" />]</li>

      </logic:iterate>

    </ul>

 

    <h3>測試2 - 疊代一個字串陣列(迴圈三次)Array [0..2]</h3>

    <ul>

      <logic:iterate id="element" name="bean" property="stringArray" indexId="index" length="3">

        <li>

        <em>

          <bean:write name="element" />

        </em> [

        <bean:write name="index" />]</li>

      </logic:iterate>

    </ul>

    <h3>測試3 - 疊代一個String Array [3..4](輸出索引從3開始的元素)</h3>

    <ul>

      <logic:iterate id="element" name="bean" property="stringArray" indexId="index" offset="3">

        <li>

        <em>

          <bean:write name="element" />

        </em> [

        <bean:write name="index" />]</li>

      </logic:iterate>

    </ul>

    <h3>測試4 - 疊代一個String Array [1..3]</h3>

    <ul>

      <logic:iterate id="element" name="bean" property="stringArray" indexId="index" offset="1" length="3">

        <li>

        <em>

          <bean:write name="element" />

        </em> [

        <bean:write name="index" />]</li>

      </logic:iterate>

    </ul>

    <h3>測試5 - 從頭至尾疊代一個Array List</h3>

    <ul>

      <logic:iterate id="item" name="list" indexId="index">

        <li>

        <em>

          <bean:write name="item" />

        </em> [

        <bean:write name="index" />]</li>

      </logic:iterate>

    </ul>

    <h3>測試6 - 疊代一個Array List [0..2]</h3>

    <ol>

      <logic:iterate id="item" name="list" indexId="index" offset="0" length="3">

        <li>

        <em>

          <bean:write name="item" />

        </em> [

        <bean:write name="index" />]</li>

      </logic:iterate>

    </ol>

    <h3>測試7 - 疊代一個Array List [2..4]</h3>

    <ol>

      <logic:iterate id="item" name="list" indexId="index" offset="2" length="3">

        <li>

        <em>

          <bean:write name="item" />

        </em> [

        <bean:write name="index" />]</li>

      </logic:iterate>

    </ol>

    <h3>測試8 - 疊代一個int array</h3>

    <ul>

      <logic:iterate id="item" name="intArray" indexId="index">

        <li>

        <em>

          <bean:write name="item" />

        </em> [

        <bean:write name="index" />]</li>

      </logic:iterate>

    </ul>

    <h3>測試 9 - 疊代一個int array [0..2]</h3>

    <ul>

      <logic:iterate id="item" name="intArray" indexId="index" length="3">

        <li>

        <em>

          <bean:write name="item" />

        </em> [

        <bean:write name="index" />]</li>

      </logic:iterate>

    </ul>

    <h3>測試10 - 疊代一個int array [2..4]</h3>

    <ul>

      <logic:iterate id="item" name="intArray" indexId="index" offset="2" length="3">

        <li>

        <em>

          <bean:write name="item" />

        </em> [

        <bean:write name="index" />]</li>

      </logic:iterate>

    </ul>

  </body>

</html>

 

下麵是TestBean.java的部分:

 

 

package org.apache.struts.webapp.exercise;

 

 

import java.util.ArrayList;

import java.util.Collection;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.util.LabelValueBean;

 

 

public class TestBean extends ActionForm {

 

//省略......................................................

 

 

    private String stringArray[] =

    { "String 0", "String 1", "String 2", "String 3", "String 4" };

 

    public String[] getStringArray() {

        return (this.stringArray);

    }

 

    public void setStringArray(String stringArray[]) {

        this.stringArray = stringArray;

    }

 

//省略........................................................

   }

 

0 留言: