读书人

struts html:select有关问题,请帮帮忙

发布时间: 2012-01-26 19:40:46 作者: rapoo

struts html:select问题,请帮帮忙
比如我在数据库中查到如下内容
id name
1001 name1
2002 name2
3003 name3

我在Action中只能用HashMap
HashMap map = new HashMap();
map.put( "1001 ", "name1 ");
map.put( "1002 ", "name2 ");
map.put( "1003 ", "name3 ");
ArrayList list = new ArrayList();
list.add(map);
request.setAttribute( "list ",list);
-------------------------------------
在JSP页面如下
<html:select property= "username ">
<html:options collection= "list " property= "id " labelProperty= "name " />
</html:select/>

select中怎么才能显示出我想要的值,在Action中只能用HashMap这类对象来存值
我该怎么实现啊


[解决办法]
HashMap map = new HashMap();
map.put( "1001 ", "name1 ");
map.put( "1002 ", "name2 ");
map.put( "1003 ", "name3 ");
ArrayList list = new ArrayList();
list.add(map);
request.setAttribute( "list ",list);


改为:
HashMap map = new HashMap();
map.put( "1001 ", "name1 ");
map.put( "1002 ", "name2 ");
map.put( "1003 ", "name3 ");
request.setAttribute( "list ",map);
[解决办法]
ArrayList arrayList = new ArrayList();

arrayList.add(new LabelValueBean( "1001 ", "name1 "));
arrayList.add(new LabelValueBean( "1002 ", "name2 "));
arrayList.add(new LabelValueBean( "1003 ", "name3 "));

request.setAttribute( "list ",arrayList);

[解决办法]
建议用jstl

struts标签不如jstl好用
[解决办法]
同意楼上的。
Map没有实现collection接口,因此要用list来传递。
而html:options标签直接与LabelValueBean关联做起来比较简单。
[解决办法]
晕,跳了一楼~
[解决办法]
我也非常同意楼上使用 LabelValueBean,这样使用比较简单而又方便。呵呵

读书人网 >Java Web开发

热点推荐