• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

关于web中下拉列表的几种实现方法

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 32次 | 进入软件测试论坛讨论

领测软件测试网

总结一下关于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 ... />

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网