关于web中下拉列表的几种实现方法
2008-03-25 13:37
579 查看
从数据库中获得数据List,将数据放到Request里面使用setAttribute(”AList”,AList)
A中有2个属性(String id,String value)
1.使用JSTL的forEach方式
<select name=”xx” ……..>
<c:forEach items="${AList}" var="p" >
<c:choose>
<c:when test="${xxx == p.id}">
<option value='<c:out value="${p.id}"/>' selected="selected">
<c:out value="${p.value}"/>
</option>
</c:when>
<c:otherwise>
<option value='<c:out value="${p.id}"/>'>
<c:out value="${p.value}"/>
</option>
</c:otherwise>
</c:choose>
<c:forEach>
</select>
2.使用struts的标签
<html:select property=”xxx”>
<html:options collection="AList" labelProperty="value" property="id" />
</html:select>
查一下struts的api文档,可以看到select 中选项有3 taglib可以使用。
第一种直接使用把所有选项写在中间。
<html:option value="0-15">0-15</html:option> <html:option value="15-20" >15-20</html:option> <html:option value="20-30" >20-30</html:option> <html:option value="20 or above">30 or above</html:option>
第二种:把选项放在一个Collection中(这里使用List).在实际项目中,更多的是可能数据来源于db,文件等。这种情况用得比较多。
<html:options collection="AList" property="value" labelProperty="label"/>把option放在list中的过程在Action中作处理//prepare the age selector list.List ageList =new ArrayList();ageList.add(new LabelValueBean("0-15","0-15"));ageList.add(new LabelValueBean("15-20","15-20"));ageList.add(new LabelValueBean("20-30","20-30"));ageList.add(new LabelValueBean("30 or above","30 or above"));request.setAttribute("AList",AList);
这里使用了LabelValueBean,可以不用的,象
<html:options collection="AList" labelProperty="value" property="id" />
只要在AList中填入的bean有value和id属性就可以
第三种,把此list 作为Form 的一个属性.
<html:optionsCollection property="AList" />
在Form 中添加AList 的setter和getter. Form中作如下处理。
//the list can be a form property.
f.setAgeList(AList);
1.从数据库中获得数据,你应该在Action里面取得数据后,将数据放到Request里面
2.数据取出来后放在一个List或Collection或Map里面,我习惯用List
3.从List或其它的容器中取数据应该用<html:options> 或<html:optionsCollection>
4.<html:options> 和<html:optionsCollection>外层必须用<html:select property="">,所以这个属性你必须在FormBean里定义
<%
Collection col=new ArrayList();
col.add(new org.apache.struts.util.LabelValueBean("北京","bj"));
col.add(new org.apache.struts.util.LabelValueBean("天津","tj"));
col.add(new org.apache.struts.util.LabelValueBean("南京","nj"));
col.add(new org.apache.struts.util.LabelValueBean("广东1","gd"));
pageContext.setAttribute("ht",col);
%>
<html:form. action="demo.do" method="post">
选择你喜欢的城市:
<html:select property="city">
<html:optionsCollection name="ht"/>
</html:select>
<br>
<html:submit value="提交"/>
<html:reset value="重置"/>
</html:form>
5.由于你要用到这些标签,所以你必须定义FormBean
6.从Action取数据,以List为例
List list = xxxxx;//从数据库中取得下拉列表中的数据
request.setAttribute("list",list);
在页面显示
<html:form action="xxxx">...<html:select property="xxx"><html:options collection="list" labelProperty="下拉框中显示的内容,一般是name或其它相似属性" property="各选项对应的值,一般是id" /></html:select>...</html:form>
A中有2个属性(String id,String value)
1.使用JSTL的forEach方式
<select name=”xx” ……..>
<c:forEach items="${AList}" var="p" >
<c:choose>
<c:when test="${xxx == p.id}">
<option value='<c:out value="${p.id}"/>' selected="selected">
<c:out value="${p.value}"/>
</option>
</c:when>
<c:otherwise>
<option value='<c:out value="${p.id}"/>'>
<c:out value="${p.value}"/>
</option>
</c:otherwise>
</c:choose>
<c:forEach>
</select>
2.使用struts的标签
<html:select property=”xxx”>
<html:options collection="AList" labelProperty="value" property="id" />
</html:select>
查一下struts的api文档,可以看到select 中选项有3 taglib可以使用。
第一种直接使用把所有选项写在中间。
<html:option value="0-15">0-15</html:option> <html:option value="15-20" >15-20</html:option> <html:option value="20-30" >20-30</html:option> <html:option value="20 or above">30 or above</html:option>
第二种:把选项放在一个Collection中(这里使用List).在实际项目中,更多的是可能数据来源于db,文件等。这种情况用得比较多。
<html:options collection="AList" property="value" labelProperty="label"/>把option放在list中的过程在Action中作处理//prepare the age selector list.List ageList =new ArrayList();ageList.add(new LabelValueBean("0-15","0-15"));ageList.add(new LabelValueBean("15-20","15-20"));ageList.add(new LabelValueBean("20-30","20-30"));ageList.add(new LabelValueBean("30 or above","30 or above"));request.setAttribute("AList",AList);
这里使用了LabelValueBean,可以不用的,象
<html:options collection="AList" labelProperty="value" property="id" />
只要在AList中填入的bean有value和id属性就可以
第三种,把此list 作为Form 的一个属性.
<html:optionsCollection property="AList" />
在Form 中添加AList 的setter和getter. Form中作如下处理。
//the list can be a form property.
f.setAgeList(AList);
1.从数据库中获得数据,你应该在Action里面取得数据后,将数据放到Request里面
2.数据取出来后放在一个List或Collection或Map里面,我习惯用List
3.从List或其它的容器中取数据应该用<html:options> 或<html:optionsCollection>
4.<html:options> 和<html:optionsCollection>外层必须用<html:select property="">,所以这个属性你必须在FormBean里定义
<%
Collection col=new ArrayList();
col.add(new org.apache.struts.util.LabelValueBean("北京","bj"));
col.add(new org.apache.struts.util.LabelValueBean("天津","tj"));
col.add(new org.apache.struts.util.LabelValueBean("南京","nj"));
col.add(new org.apache.struts.util.LabelValueBean("广东1","gd"));
pageContext.setAttribute("ht",col);
%>
<html:form. action="demo.do" method="post">
选择你喜欢的城市:
<html:select property="city">
<html:optionsCollection name="ht"/>
</html:select>
<br>
<html:submit value="提交"/>
<html:reset value="重置"/>
</html:form>
5.由于你要用到这些标签,所以你必须定义FormBean
6.从Action取数据,以List为例
List list = xxxxx;//从数据库中取得下拉列表中的数据
request.setAttribute("list",list);
在页面显示
<html:form action="xxxx">...<html:select property="xxx"><html:options collection="list" labelProperty="下拉框中显示的内容,一般是name或其它相似属性" property="各选项对应的值,一般是id" /></html:select>...</html:form>
相关文章推荐
- 关于web中下拉列表的几种实现方法
- 关于web中下拉列表的几种实现方法
- 关于web中下拉列表的几种实现方法
- web中下拉列表的几种实现
- web中下拉列表的几种实现(转载)
- JQuery 使用attr方法实现下拉列表选中
- javascript实现在下拉列表中显示多级树形菜单的方法
- Web服务的几种实现方法
- jQuery实现在下拉列表选择时获取json数据的方法
- 二级联动菜单动态刷新下拉列表的一种实现方法
- 给自定义控件(Web Control)添加事件的几种方法。前两种方法可以不实现IPostBackEventHandler
- 层叠选择框的实现(实现方法参照本博客的下拉列表)
- 关于python单例的常用几种实现方法
- 用C语言关于学生管理系统的几种实现方法(一位数组,二维数组,指针,结构体)
- web打印实现几种方法
- javascript实现在下拉列表中显示多级树形菜单的方法
- Webpack实现按需打包Lodash的几种方法详解
- 关于spinner自定义下拉列表(两种方法)
- 可编辑Select下拉列表控件实现方法(非DIV模拟)
- 关于批量选择新增成员到群组列表的实现方法。