Forrest Gump
===========================================================
如何从struts标签logic:iterator中取List中的对象的属性,并动态选择checkbox呢
===========================================================
verified correct

如何从struts标签logic:iterator中取List中的对象的属性,并动态选择checkbox呢
说明
A 类型为ArrayList的persons变量中存放了所有的Person类.
B String类型的数组selectedItems存放的是所有被选中的id的值.

jsp完整代码如下:
<html:form action="/myJsp" >
<bean:define id="personList" name="myJspForm" property="persons" type="java.util.List" />
<logic:iterate id="person1" name="personList" type="org.myProject.base.Person" >
<html:multibox property="selectedItems" name="myJspForm" >
<bean:write name="person1" property="id" />
</html:multibox>
<bean:write name="person1" property="name" />
</logic:iterate>
<html:submit value="submit"/><html:cancel/>
</html:form>

1 首先用struts的bean:define标签从Form中取出List:
<bean:define name="myForm" property="persons" id="personList"
其中name="myForm"中的myForm你必须首先在struts-config.xml中有定义:
<form-beans>
<form-bean name="myJspForm"
type="org.myProject.struts.form.MyJspForm">
</form-bean>
</form-beans>
而property="persons"则在MyForm中定义了private ArrayList persons;与他的getter和setter方法。
当然你们也可以直接在Action里将List放进request.setAttribute中,然后直接取。
2 取出List以后就可以用struts标签logic:iterator来循环取放在List中的对象Person类了。
<logic:iterate id="person1" name="personList" type="org.myProject.base.Person" >
其中name是上面定义List的id相对应,这里不用加property,使用type,就是你在里面放的对象的类的完整路径。
3 然后使用struts标签html:multibox与Form里的数组selectedItems相对应.
<html:multibox property="selectedItems" name="myJspForm" >
其中selectedItems在Form里如下定义private String[] selectedItems;与他的getter和setter方法.
4 现在用struts标签bean:write取出Person类的id与multibox中的值对应,相同的就会被打上勾选中
<bean:write name="person1" property="id" />
就可以了.
fesir 发表于:2007.08.31 14:47 ::分类: ( Java/J2EE/EJB/Ajax/Struts ) ::阅读:(900次) :: 评论 (0)

发表评论
标题

在此添加评论
表情符号: smile laughing tongue angry crying sad wassat wink

称呼

邮箱地址(可选)

个人主页(可选)

 authimage


切换风格
新闻聚合
博客日历
文章归档...
最新发表...
最新评论...
最多阅读文章...
最多评论文章...
博客统计...
Blog信息
网站链接...