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;
}
}
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>
<nested:text property="year"/>
<nested:text property="month"/><br/>
</nested:iterate>
ok,就这么简单!