下面介绍几种在struts中常用的拉列表的显示方式
1 利用HashMap创建下拉列表
HashMap animals = new HashMap();
animals.put(new Integer(1), "Cat");
animals.put(new Integer(2), "Dog");
animals.put(new Integer(3), "Horse");
animals.put(new Integer(4), "Rabbit");
animals.put(new Integer(5), "Goldfish");
request.setAttribute("animals", animals);
<html:select property="animal" size="5">
<html:optionsCollection name="animals" value="key" label="value" />
</html:select>
2 利用struts的LabelValueBean类创建下拉列表
List<LabelValueBean> lstInterest = new ArrayList<LabelValueBean>();
hobbyList.add(new LabelValueBean("游泳","1");
hobbyList.add(new LabelValueBean("网球","2");
hobbyList.add(new LabelValueBean("羽毛球","3");
lstInterest 是userListForm中的一个属性
<html:select property="interest" name="userListForm">
<html:optionsCollection name="userListForm" property="lstInterest" />
</html:select>
浏览器解析后的代码:
<html:select property="interest">
<html:option value="1">游泳</html:option>
<html:option value="2" selected>网球</html:option>
<html:option value="3">羽毛球</html:option>
</html:select >
3 固定显示的下拉列表
<html:select property="sex" >
<html:option value=""> </html:option>
<html:option value="1">男</html:option>
<html:option value="0">女</html:option>
</html:select>
4 用c标签写的下拉列表
// 年的下拉列表:显示1960年到1998年
<html:select property="strYear" οnchange="changeDay();" >
<c:forEach var="y" begin="1960" end="1998">
<html:option value="<c:out value="${y}"/>"><c:out value="${y}"/></html:option>
</c:forEach>
</html:select>
// 月的下拉列表:显示1月年到12月
//当月份值是1位数值时,给此值前面补0,故使用了 <c:choose>标签
<html:select property="strMonth" οnchange="changeDay();">
<c:forEach var="month" begin="1" end="12">
<c:choose>
<c:when test="${month < 10}">
<html:option value="0<c:out value="${month}"/>"><c:out value="${month}" /></html:option>
</c:when>
<c:otherwise>
<html:option value="<c:out value="${month}"/>"><c:out value="${month}" /></html:option>
</c:otherwise>
</c:choose>
</c:forEach>
</html:select >
/************************************************************/
//这里我们使用javascript方法动态在客户端构建日期下拉列表
<html:select property="strDay">
</html:select >
js文件:
function changeDay(){
var num = 31;
// 需要自己动手写一段代码,根据年月来给num变量付值
setDates(num);
}
//构建1-num的下拉列表
function setDates(num){
for(var i = 1;i<= num;i++){
if(i<10){
var option = new Option(i,'0'+i);
}else{
var option = new Option(i,i);
}
document.formName.strDay.options[i]=option;
}
}
/************************************************************/
初次发贴,错误之处,请谅解!