Repeating multiple Form Fields using Struts 1.02
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Raj_K
Posted On:   Wednesday, December 11, 2002 02:27 PM

I have properties in my ActionForm which are Arrays and I do not know the size of the array until the Action is executed. How do I implement this. I believe that in struts 1.1, I can use nested tags and indexed properties but how do I do it in struts 1.0? can I use nested forms? is there a way to do it.

Thanks.

Re: Repeating multiple Form Fields using Struts 1.02

Posted By:   Ron_Monson  
Posted On:   Friday, December 13, 2002 10:53 AM

Raj,




Here is a methodology I use. When I want to "nest" a
collection of form beans in another form bean I
implement it as a List. This requires a total of 4
getters/setters. Below is the source ofr an example of
a form bean representing a 'Person' which can have
many 'Addresses'.





package mypackage;

import org.apache.struts.action.*;
import java.util.List;
import java.util.ArrayList;
import mypackage.AddressForm;


public class PersonForm extends ActionForm
{
private List addresses;


public AddressForm getAddress(int index) {
checkListSize(index, addresses);
return addresses.get(index);
}


public List getAddresses() {
if (addresses == null)
addresses = new ArrayList();

return addresses;
}


public void setAddress(int index, AddressForm in) {
checkListSize(index, addresses);
addresses.set(index, value);
}


public void setAddresses(List in) {
addresses = in;
}


private void checkListSize(int index, List list) {
if (list == null)
list = new ArrayList();

try {
for (int i = list.size(); i <= index; i++)
list.add(i, null);
}
catch (Exception e) {
//Handle exception
}
}

}






This will probably need a little tweaking on your end.
The main thing to worry about is making sure the List
never generates exceptions. Part of the problem is the
way that request parameters are received in random
order. The
checkListSize
will always ensure
that everything is taken care of.




Here is an example of JSP code to use this technique:










,








Hope this helps.
About | Sitemap | Contact