神秘的 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)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 吉林市| 宝坻区| 东明县| 高平市| 广平县| 河池市| 阿城市| 合山市| 久治县| 水城县| 壶关县| 惠来县| 嘉黎县| 阿克陶县| 闵行区| 长乐市| 永新县| 绥棱县| 长宁区| 灵丘县| 普定县| 潮州市| 锡林浩特市| 黎城县| 宁陕县| 河南省| 张家口市| 阳春市| 泰来县| 凤冈县| 河北省| 仙桃市| 镇雄县| 长兴县| 嘉鱼县| 双柏县| 习水县| 黑水县| 若尔盖县| 湾仔区| 富平县|