网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>专题栏目>>STRUTS专题>查看文档  
  web中下拉列表的几种实现     
  文章作者:未知  文章来源:未知  
  查看:321次  录入:管理员--2006-09-28  
 

总结一下关于web上使用下拉框的情况

从数据库中获得数据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里定义
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>



补充一点点:

因为数据你要从 数据库去取, 所以一般在 action 里调用 DAO ,作为 request 的一个属性传到页面上; 这时一般用 <html:options .../> 标签

另外,如果数据不从数据库去取,而是代码固定的,则一般把这种放到 ActionForm 里,作为属性在页面上取,这时一般用 <html:optionsCollection ... />

 
 
上一篇: struts源代码阅读(Commons-Pool包)    下一篇: 推荐-扩展Struts
  相关文档
Struts HOW-TO 系列 之 数据库访问 09-28
用Struts建立MVC应用的介绍 09-27
类反射简化Struts应用程序的开发 09-28
Struts中的MVC 09-28
正确优雅地解决用户退出——JSP及Struts解决方案 09-28
struts框架下,在jsp页面显示XSL格式化的XML 09-28
在Eclipse中开发struts应用程序 10-10
为Web服务构建Struts应用程序 09-27
Struts 增加Validator 09-28
Struts,MVC的一种开放源码实现 09-27
关于VO、PO的理解 09-28
用JBuilder9 开发Struts实例 09-28
Struts处方:Hibernate与Struts 09-28
Struts—基于MVC设计模式的JSP 09-28
在Struts中用JFreeChart生成图表 09-28
推荐-扩展Struts 09-28
学习Jakarta Struts(三) 09-28
Struts用户和开发指南(前言之二) 09-28
struts学习心得 09-28
使用ActionForm一次获取表单的所有参数 09-28
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
厦门(总部):13616026886 福州:0591-87655121
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息