Struts Help: Saving a dynamic number of list items using nested tags

K

Katie Wright

Hi,

I am not new to struts but I am new to the more advanced tags. I have
run into this problem. I think that I am close but just not
understanding a key concept.

I am trying to create an HTML form like so:

Item1Label Item1Textbox
Item2Label Item2Textbox
Item3Label Item3Textbox

What is new to me is the fact that in this case, the number of items
vary and the item labels vary.

I can display the page fine with the populated values for edit but
when I try to do a save, I get the following struts exception:

javax.servlet.ServletException:BeanUtils.populate
at org.apache.struts.util.RequestUtils.populate
RequestUtils.java:1254)
at org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:821)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:254)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)

It seems to be having trouble activating my setter in my form but not
sure why. I think because I have setup something improperly but don't
know what.

I have done some research on the Internet and by following by example
I have developed the following:
<html:form action="MainFormSave.do">
<nested:iterate property="itemList">
<nested:hidden property="itemName" />
<nested:write property="itemName" />
<nested:text property="itemDescription" />
</nested:iterate>
</html:form>

I have a MainForm class which includes the following:

private ArrayList _itemList = new ArrayList();

public void setItemList(int index, ItemBean item) {
if (index < this._itemList.size()) {
this._itemList.set(index, item);
} else {
this._itemList.add(item);
}
}
public ArrayList getItemList() {
return (this._itemList) ;
}

And I have an ItemBean which includes the following:

private String _itemName = null;
private String _itemDescription = null;

public void setItemName(String name) {
this._itemName=name;
}

public String getItemName() {
return (this._itemName) ;
}

public void setItemDescription(String desc) {
this._itemDescription=desc;
}

public String getItemDescription() {
return (this._itemDescription) ;
}

Thanks in advance,

Katie Wright (who is hoping that it is not something completely
obvious)
(e-mail address removed)
 
K

Katie Wright

At the risk of appearing like I am having a conversation with myself
:), I wanted to say that I resolved this problem.

I wanted to avoid this but I ended up having to download the struts
source then compiling it with additional debugging calls and then
repackaging the struts jars.

I did a couple of things so I am not sure which item fixed the
problem.

I made my Bean implement Serializable. I don't think that this was
the problem.

When debugging I found that I was getting an IndexOutofBounds
exception because the list was of size 0. I removed the constructor
and moved the initialization to when the variable is declared. I
removed the line in the reset() which was setting the list to an empty
list.

Hope this helps someone out there someday who searches newsgroup
archives for assistance like I do.

Katie Wright
(e-mail address removed)
 
Joined
Mar 28, 2010
Messages
1
Reaction score
0
Hi,

I also facing similar problem. I used ur solutions, but didnt get suceess.

few details regarding my code :

==>> [1] ItemForm implements Serializable {
.........
String itemcode;
String itemname;
.......
}

==>>[2] GRNForm extends ActionForm {

...................
String grncode;
List<ItemForm> itemForm;
...................

//constructor
public GRNForm (){
itemForm = new ArrayList();
}
}

==>>[3] in JSP, I added ItemDetails in Form of table

<html:form action="addGRN.do" >
.......
......
<table>
<nested:iterate property="itemForm">
.......
<td>
<nested:text property="itemcode" size="5"></nested:text>
</td>
<td>
<nested:text property="itemname" size="15"></nested:text>
</td>
......
</nested:iterate>
</table>

.......
......

==>> Error Occurs

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.apache.commons.beanutils.PropertyUtilsBean.getIndexedProperty(PropertyUtilsBean.java:433)
at org.apache.commons.beanutils.PropertyUtilsBean.getIndexedProperty(PropertyUtilsBean.java:340)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:684)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:884)


Can you pl. help me?

Thanks in Advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top