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

來源:互聯網
上載者:User

 

action中代碼如下:<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->        List allUser=this.getLoginServiceImpl().find();
        request.setAttribute("user", allUser);
        return mapping.findForward("listUser");

註:其中User對象有id和name屬性

jsp顯示:法一<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->            <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顯示:法二<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->       <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頁面:<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><html:select property="school">
<html:optionsCollection name="userForm"
      property="userList"/>
</html:select>

2.與集合配合使用
action中<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->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頁面<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><html:select property="school">
<html:optionsCollection name="userList"
      label="label" value="value"/>
</html:select>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.