Hi Junilu,
You are right. I got your suggestion and create a working example. Thks.
master bean:
public final class A extends ActionForm {
private ArrayList allB = new ArrayList();
public ArrayList getAllBList() {
return allB;
}
public void setAllBList(ArrayList list) {
allB = list;
}
public B getAllB(int index) {
while (index >= allB.size()) {
allB.add(new B());
}
return (B) allB.get(index);
}
public void setAllB(int index, B in) {
if (index >= allB.size()) {
int i = allB.size();
do {
allB.add(new B());
} while (index >= ++i);
}
allB.set(index, in);
}
}
Detail Bean:
public final class B extends ActionForm {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
private
String field = sdf.getInstance().format(new Date());
/**
* Returns the value of field.
*
*@return The field value
*/
public String getField() {
return field;
}
/**
* Sets the value of field.
*
*@param field The value to assign field.
*/
public void setField(String field) {
this.field = field;
}
}
Simple Action:
public final class AAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
A f = (A) form;
B b = new B();
ArrayList al = f.getAllBList();
f.setAllB(al.size(), b);
return (mapping.findForward("input"));
}
}
Simple JSP:
<nested:iterate id="allB" property="allBList" name="AForm">
<nested:text indexed="true" property="field" name="allB"/>
</nested:iterate>
Originally posted by Junilu Lacar:
Corey,
The problem is probably in the setAllComment(int index) method. First check (index >= allComments.size()). If it is, use a loop to add the appropriate number of new Comments. Then you can safely return allComments.get(index). You also might want to write a method resizeAllComments(int newSize) and call it from both the getAllComment and setAllComment methods when the index is out of bounds.