Struts2中的select标签

今天有毕业生问了一个问题:

 


 

 

我在Dao层使用了public List findAllIDandName() {

  List templateIDandName = this.getHibernateTemplate().find("select tem.id,tem.templatename   from Template tem");

  return templateIDandName;

 }

返回的List中的存储的数据结构是:[[1, weew], [2, GenPD20110425225339171F.java], [3, colo20110426000712562r.htm], [4, GenPD20110426001233531F.java], [5, GenPD20110426002055750F.java], [6, colo20110426003445843r.htm], [7, iTextAP20110426003712265I.doc], [8, colo20110426003850671r.htm], [9, GenPD20110426004353687F.java], [10, GenPDFActio20110426005243031n.java], [11, struts2标签详20110426010817546解.doc], [12, 济南大学优秀学生干部登记表(窦新春)20110426010909906.doc], null, null, null, null]。

现在我想在页面上使用<s:select list="temList" label="请选择要使用的模板" value="temList[0]"></s:select>  想让listkey为id,listvalue为templatename。按照目前这种情况,下拉列表的内容是id。像这种由HIbernateDaoSupport find()方法返回的List类型,我我应该怎么在页面中,获取到list中的具体的数据啊?

 


 

呵呵,比较长,其实很简单,就是怎样用struts2的select标签获取action传递过来的list,我的回复:
刚看到你的留言,不好意思,我理解你想把list返回给页面,这个struts2标签中是可以实现的<s:select name="dl.id" list="templist" listKey="id" listValue="xh" label="选择学生"/>,这个是我以前上课时写过的一个例子,其中templist就是从action传递过来的list,需要做的准备工作:
1、Action类中,需要定义一个private List<Dlb> templist; 并有templist的getter和setter方法,其中Dlb是我的一个pojo类,里面有两个属性id和xh,还有属性的getter、setter方法。注意:泛型是必须的,Dlb是一个登陆用的类。
2、你从dao中find回来的list,赋值给templist(通过templist的setter方法)。
3、select标签中,list中名字必须跟action中定义的templist一样,同时listKey必须个Dlb类中的属性id一致,listValue必须和Dlb中的xh属性一致,这样就能传递了。

对应你的例子,我大概给你写一下:
首先,action中定义个temList:  private List<Template> temList;
其次,在action中对应方法里,把findAllIDandName()返回的list,通过setTemList()这个方法给temList赋值;
最后在页面里<s:select name="templateid" list=" temList" listKey=" id" listValue=" templatename" label="请选择要使用的模板"/>

补充一点,一般填充下拉菜单,我习惯让action类实现Preparable接口,然后实现prepare方法,至于为什么,什么时候需要这样做,大家可以做做实验,考虑一下。

这样能看明白否?

 


版权声明:本文为itjoy原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。