<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    yunye 的 JavaBlog

    @see codemouse

    統計

    留言簿(1)

    閱讀排行榜

    評論排行榜

    logic:iterate(2)

    <logic:iterate> 是Logic 標簽庫中最復雜的標簽,也是用途最廣的一個標簽,它能夠在一個循環中遍歷數組、Collection、Enumeration、Iterator 或 Map 中的所有元素。

    1. 遍歷集合
      <logic:iterate> 的 name 屬性指定需要進行遍歷的集合對象, 它每次從集合中檢索出一個元素, 然后把它放在page 范圍內, 并以id 屬性指定的字符串來命名這個元素, 例如:
        <%
            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>
        以上代碼先定義了一個Vector 類型的集合變量 Animals, 它存放在request 范圍內. 接下來<logic:iterate> 標簽在一個循環中遍歷Animals 集合(這個集合名就是在標簽中的name 屬性的值)中所有元素, 每次檢索到一個元素, 就把它命名為"element"(標簽id 屬性的值), 并存放在page 范圍內.
        在<logic:iterate> 中, 還嵌套了一個<bean:write>標簽, 它用于輸出每個元素的內容. 以上代碼的輸出內容如下:
        Dog
        Cat
        Bird
        Chick    
         length    屬性指定需要遍歷的元素的數目, 如果沒有設置length 屬性,就遍歷集合中的所有元素.
        offset      屬性指定開始遍歷的起始位置, 默認值為 "0" , 表示從集合的第一個元素開始遍歷.
        indexId  屬性定義一個代表當前遍歷元素序號的變量, 這個變量被存放在 page 范圍內, 可以被標簽主體的
    <bean:write> 標簽訪問. 例如:

        <logic:iterate 
                id="element"           // 指定輸出元素的名 與 <bean:write> 中name 屬性一致
                indexId="index"       // 遍歷元素序號的變量, 這個變量放在page 范圍內
                name="Animals"    // request 中的集合名, 從中取循環取出元素
                offset="1"                 // 從集合的第 2 條記錄開始取數
                length="2">             // 取出 2 個元素
            <bean:write name="index"/>.              // 輸出每個元素的序號, 與indexId 的屬性一致
            <bean:write name="element"/><br>  // 輸出每個元素的內容, 與id 的屬性一致
        </logic:iterate>

    2. 遍歷Map
        <logic:iterate> 標簽還可以遍歷HashMap 中的元素,

    例如:
        <%
            HashMap months = new HashMap();
           
            months.put("Jan","January");
            months.put("Feb","February");
            months.put("Mar","March");
           
            request.setAttribute("month", months);
        %>
        <logic:iterate id="element" indexId="ind" name="months">
            <bean:write name="ind"/>.                                          // 序號
            <bean:write name="element" property="key"/>:      // 鍵名
            <bean:write name="element" property="value"/>    // 鍵值
        </logic:iterate>
        以上代碼先定義一個名為"months" 的HashMap, 存放在request 范圍內. 接下來在<logic:iterate> 標簽遍歷months 對象的每一個元素, 每一個元素包含一對 key/value . 在<logic:iterate> 標簽主體中包含三個<bean:write> 標簽, 分別輸出每個元素的序號、key 和 value. 以上代碼的輸出內容如下:
        0.Mar: March
        1.Feb: February
        2.Jan: January
        如果HashMap 中的每個元素的 value 是集合對象,

    則可以采用嵌套的<logic:iterate>標簽遍歷集合中的所有對象, 例如:
        <%
               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);
        %>
        <logic:iterate   id="element"              // 與<bean:write> 中的name 屬性對應, 輸出內容
                                  indexId="ind"              // 與<bean:write> 中的name 屬性對應, 輸出序號
                      &nbsp;           name="catelog">      // 指定輸出元素的名稱
            <bean:write name="ind"/>.           // 輸出序號
            <bean:write name="element"      // 與<logic:iterate>中id 屬性對應
      property="key"/>    // 集合中的鍵名

            <logic:iterate
                    id="elementValue"   // 與<bean:write> 中的name 屬性對應
                    name="element"      // 指定輸出元素的名稱
                    property="value"       // 集合中的鍵值
                    length="3"                  // 取3 個元素
      offset="1">                               // 從第 2 個位置取
         -------<bean:write name="elementValue"/>
     </logic:iterate>

        </logic:iterate>


        以上代碼先定義一個名為"catelog" 的HashMap , 存放在request 范圍內,它的每個元素的value 為字符串數組.
        接下來外層的<logic:iterate>標簽遍歷HashMap 中的所有元素, 內層的<logic:iterate>標簽訪問每個元素的value 屬性, 遍歷value 屬性引用的字符串數組中的所有元素.

    3. 設置被遍歷的變量
        可以通過以下方式來設置需要遍歷的變量
        (1) 設置name 屬性, name 屬性指定需要遍歷的集合或Map, 例如:
            <logic:iterate id="element" name="Animals">
                <bean:write name="element"/>
            </logic:iterate>
        (2) 設置name 屬性和property 屬性, name 屬性指定一個JavaBean, property 屬性指定JavaBean 的一個屬性, 這個屬性為需要遍歷的集合或Map, 例如:
            <logic:iterate id="element" indexId="ind" name="catelog">
                <bean:write name="ind"/>
                <bean:write name="element" property="key"/>
                <logic:iterate id="elementValue" name="element" property="value" length="3" offset="1">
                    --------<bean:write name="elementValue"/>
                </logic:iterate>
            </logic:iterate>
        (3) 設置collection 屬性,collection 屬性指定一個運行時表達式, 表達式的運算結果為需要遍歷的集合或Map, 例如:
           
            <logic:iterate id="header" collection"<%=request.getHeaderNames()%>">
                <bean:write name="header"/>
            </logic:iterate>

    4. 讀取JavaBean 中的數據
      (1) 在Jsp 頁面中加入JavaBean 如:
            <jsp:useBean id="articleClasses" class="com.GetArticleClasses"/>
          上面這個JavaBean 要求必須存在一個集合數組對象,如Vector,Collection,ArrayList 等;在這個JavaBean 的構造函數中,取得數據

    庫中的數據,并將其存入數組對象中。
      (2) 使用<logic:iterate> 標簽,取出JavaBean 中存放的數組對象中的數據

            <logic:iterate
                      id="aClasses"                   //  id   : 給檢索出的元素所命的名.
                      name="articleClasses"   //  name : JavaBean 在頁面中所設置的引用ID.
                      property="coll">                 //  coll : JavaBean 中的集合數組屬性名稱.
                <tr> 
                    <td onMouseOver="this.bgColor=''#FFFFFF'onMouseOut="this.bgColor=''''">&nbsp;&nbsp;&nbsp;&nbsp;
                 <html:link  page="/articleListAction.do"
                                     paramId="classId" 
                                     paramName="aClasses"
                                     paramProperty="classId">
              <bean:write name="aClasses"       // 與<logic:iterate> 標簽中的id 屬性相對應
                           property="className" />     // 取出JavaBean中, 存放在集合對象中的,對象的className 屬性值
                 </html:link>
             </td>
                </tr>
            </logic:iterate>
      (3) 在JavaBean 中的集合對象中存放實體對象的語句如下:
            ......
            public class GetArticleClasses
            {
                // 數據集合
                private Collection coll;
       
     &nbsp;          // 返回數據集合
                public Collection getColl()
                {
                    return coll;
                }
                // 構造函數, 取出數據,存入集合中
                public GetArticleClasses()
                {
                    coll = new ArrayList();
                    try{
                        // 數據庫連接
                        Connection connection = DBConnection.getConnection();
                        if(connection != null)
                        {
                            Statement statement = connection.createStatement();
                            ResultSet resultset;
                            ArticleClass articleclass;
                            resultset = statement.executeQuery("SELECT * FROM table ORDER BY id");
             &nbsp;              while( resultset.next())
                            {
                                articleclass = new ArticleClass();
                                articleclass.setId(resultset.getInt("id"));
                                articleclass.setClassId(resultset.getString("class"));
                                articleclass.setClassName(resultset.getString("name"));
                               
                                coll.add(articleclass))
                    }

                    resultset.close();
                    connection.close();
                } else {
                    coll = null;
                }
            } catch(Exception exception) {
                coll = null;
            }
    }
    }

    posted on 2008-03-22 00:55 yunye 閱讀(449) 評論(0)  編輯  收藏 所屬分類: Struts


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 国产福利免费在线观看| 成人免费的性色视频| 亚洲国产精品人人做人人爱| 亚洲av日韩aⅴ无码色老头| 久久久www成人免费毛片| 中文字幕无码精品亚洲资源网久久| h视频在线观看免费完整版| 亚洲a级片在线观看| 无码一区二区三区免费视频| 亚洲国产精品无码中文lv| 国产精品免费播放| 精品国产亚洲AV麻豆| 亚洲综合精品网站在线观看| 国产国产人免费人成成免视频| 亚洲日本乱码在线观看| 免费人成网站在线观看不卡| 亚洲精品国产专区91在线| 毛片基地免费观看| 精品女同一区二区三区免费播放| 亚洲国产精品碰碰| 日韩在线永久免费播放| 亚洲人和日本人jizz| 午夜免费福利在线| 国产一级a毛一级a看免费视频| 日韩亚洲Av人人夜夜澡人人爽| 青青青国产在线观看免费网站| 久久久久亚洲国产AV麻豆| 亚洲欧洲一区二区三区| 最近免费中文字幕高清大全 | 中文字幕免费在线| 亚洲男人天堂2018av| 亚洲综合最新无码专区| 最近2019中文字幕免费直播| 亚洲Av无码国产一区二区| 亚洲精品无码av人在线观看| 日韩吃奶摸下AA片免费观看| 男女啪啪免费体验区| 亚洲日本国产精华液| 亚洲天堂免费在线视频| 久久久久久精品免费看SSS | 国产黄色片在线免费观看|