ActionForm中的一个ArrayList, 页面上可以用<html:iterator>实现循环显示, 但是怎么样才能在提交时使页面上这些循环显示的数据自动提交呢?
这里主要用到了common-collection包.
FormBean:
代码
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.Factory;
import org.apache.commons.collections.list.LazyList;
import org.apache.struts.action.ActionForm;
import com.yourcompany.struts.action.UserVo;

public class TestForm extends ActionForm ...{

public TestForm() ...{
Factory factory = new Factory() ...{ 
public Object create() ...{
return new UserVo();
}
};
this.contents = LazyList.decorate(new ArrayList(), factory);
}
// --------------------------------------------------------- Instance Variables
private List contents;
// --------------------------------------------------------- Methods

public List getContents() ...{
return contents;
}

public void setContents(List contents) ...{
this.contents = contents;
}
}如上,ActionForm中的构造函数是必须的.
JSP:
代码
<nested:iterate property="contents" indexId="index">
<nested:text property="year"/>
<nested:text property="month"/><br/>
</nested:iterate>
ok,就这么简单!
本文介绍如何在Struts框架中使用LazyList实现在页面上循环显示数据,并确保这些数据能在提交时被自动捕获。通过自定义工厂创建UserVo对象并填充到ActionForm的ArrayList中。
2005

被折叠的 条评论
为什么被折叠?



