1. 先写一个存放选项的bean:
package com.lizongbo.common.basic;
public class HtmlSelectStruct {
private String label;
private String value;
public HtmlSelectStruct() {
}
public HtmlSelectStruct(String label, String value) {
this.setLabel(label);
this.setValue(value);
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
2.生成下拉列表:
例如
public List listAllUserHtmlSelect() {
List l = listAllUser();
List rs = new java.util.ArrayList();
for (int i = 0; i < l.size(); i++) {
User u = (User) l.get(i);
HtmlSelectStruct hsall = new HtmlSelectStruct(u.getid()+" / "+u.getName(),
String.valueOf(u.getid()));
rs.add(hsall);
}
return rs;
}
3.放到session中,放到request里也可以
request.getSession().setAttribute("userHtmlSelect",
srl.listAllUserHtmlSelect());
//如果是null的,页面上会报错,这点不太爽
4.网页上的使用
<html:select property="userid">
<html:option value="">不选择用户</html:option>
<html:options collection="userHtmlSelect" property="value" labelProperty="label"/>
</html:select>