神秘的 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 月芽兒 閱讀(6485) 評論(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)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 黔西| 福建省| 抚松县| 丰镇市| 哈巴河县| 普兰店市| 福安市| 甘南县| 棋牌| 三台县| 桂阳县| 北安市| 皋兰县| 汉寿县| 赤城县| 常德市| 航空| 广州市| 乐亭县| 象山县| 象州县| 板桥市| 将乐县| 长寿区| 西和县| 库伦旗| 南丰县| 合山市| 洛南县| 墨玉县| 房产| 卓尼县| 越西县| 安化县| 祁门县| 新民市| 盖州市| 托里县| 金塔县| 博白县| 永春县|