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>