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

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 十堰市| 德化县| 鸡东县| 龙里县| 杭锦后旗| 宁海县| 泸水县| 彰化县| 博野县| 师宗县| 呼和浩特市| 夏邑县| 宜兰县| 米泉市| 安岳县| 淮滨县| 张家界市| 德阳市| 诸城市| 景洪市| 浏阳市| 呼伦贝尔市| 霸州市| 临潭县| 曲水县| 武城县| 洞头县| 苍溪县| 无为县| 陇南市| 万安县| 塔城市| 大荔县| 平定县| 冕宁县| 竹北市| 南投市| 高碑店市| 盈江县| 萨嘎县| 江达县|