日期:2014-05-18  浏览次数:20700 次

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,这样使用比较简单而又方便。呵呵