神秘的 J2ee 殿堂

          ·古之學者必有師·做學者亦要做師者·FIGHTING·

          jsp通過logic:iterate或html:optionsCollection循環顯示action中傳過來的對象List

          action中代碼如下:
                  List allUser=this.getLoginServiceImpl().find();
                  request.setAttribute(
          "user", allUser);
                  
          return mapping.findForward("listUser");
          注:其中User對象有id和name屬性

          jsp顯示:法一
                      <html:select property="school">
                      
          <html:option value="">
                          
          <bean:message key="login.select" />
                     
          </html:option>
                          
          <logic:present name="user">
                              
          <logic:iterate id="user" name="user" offset="0">
                                  
          <option value="<bean:write name="user" property="id" />">
                                      
          <bean:write name="user" property="name" />
                                  
          </option>
                              
          </logic:iterate>
                          
          </logic:present>
                  
          </html:select>
          jsp顯示:法二
                 <html:select property="school">
                     
          <html:optionsCollection name="user" value="id" label="name"/>
                 
          </html:select>

          optionsCollection標簽用法:
          與options標簽一樣,optionsCollection標簽可以從集合或者是包含集合的對象里獲得選項的標簽/值對。在這兩種情況里,集合或包含集合的對象必須是一個作用域對象,否則定制標簽將無法訪問它。

          1.與包含集合的對象配合使用
          舉例:userForm動作表單有一個如下所示的ArrayList類型的userList屬性,相應的set,get方法,通過在action中設置好userForm后,request.setAttribute("userForm",userForm);
          在jsp頁面:
          <html:select property="school">
          <html:optionsCollection name="userForm"
                property
          ="userList"/>
          </html:select>

          2.與集合配合使用
          action中
          ArrayList userList=new ArrayList();
          userList.add(
          new LabelValueBean("1","haha"));
          userList.add(
          new LabelValueBean("2","dada"));
          userList.add(
          new LabelValueBean("3","xiaoxiao"));
          request.setAttribute(
          "userList",userList);
          jsp頁面
          <html:select property="school">
          <html:optionsCollection name="userList"
                label
          ="label" value="value"/>
          </html:select>

          (注:邊學邊share,如有問題,歡迎交流學習)


          posted on 2007-09-15 23:58 月芽兒 閱讀(6487) 評論(3)  編輯  收藏 所屬分類: J2EE學習心得

          評論

          # re: jsp通過logic:iterate或html:optionsCollection循環顯示action中傳過來的對象List 2008-03-26 20:11 李曉勇

          今天剛好碰上類似問題,此文幫我解開了疑惑,多謝!!!  回復  更多評論   

          # re: jsp通過logic:iterate或html:optionsCollection循環顯示action中傳過來的對象List 2009-05-09 13:07 過冬的kimera

          幫大忙了,謝謝  回復  更多評論   

          # re: jsp通過logic:iterate或html:optionsCollection循環顯示action中傳過來的對象List 2012-05-30 15:25 cowrylee

          <option value="<bean:write name="user" property="id" />">
          這種寫法不符合xhtml  回復  更多評論   

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 唐河县| 锦屏县| 荆门市| 闸北区| 平凉市| 乌鲁木齐市| 大田县| 赣州市| 顺昌县| 浑源县| 左贡县| 新宁县| 漳平市| 山阳县| 林芝县| 含山县| 陇南市| 富锦市| 平山县| 进贤县| 多伦县| 莱州市| 渝北区| 香港 | 洛宁县| 洛隆县| 牡丹江市| 丹江口市| 镇远县| 白银市| 洪泽县| 高安市| 都昌县| 丹江口市| 全州县| 平顶山市| 通州市| 松原市| 花莲县| 盐津县| 胶州市|