How to populate a ArrayList FormBean attribute?

E

Eduardo

Hi!
I'd like to get some help!

I have an ActionForm similar to the following:

public class Client extends ActionForm {
private String name;
private ArrayList phoneNumber;

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

public String getName(){
return name;
}

public void addPhoneNumber(Phone phone){
phoneNumber.add(phone);
}

...
}

This is the simple Phone bean...
--------------------------------
public class Phone {
private String phoneType; // Commercial/Particular/Mobile/etc.
private String countryCode;
private String localCode;
private String phoneNumber;

public void setPhoneType(String phoneType){
this.phoneType = phoneType;
}

....

}

The question is: How can I populate my actionForm from an HTML form?
How can I access the add method?

Thanks a lot,

Eduardo
 
S

Sudsy

Eduardo wrote:
The question is: How can I populate my actionForm from an HTML form?
How can I access the add method?

You can't. Seriously, Struts looks for setXXX methods only. You
should be using indexed properties. Check the javadocs for details.
Hint: For a field named phoneNumber, you should have a method with
the signature setPhoneNumber( int index, Object o ). You also might
wish to read an article I've written on the subject. It can be found
here: <http://www.sudsy.net/technology/struts-advanced.html>
 
J

John Raab

I had a problem using ArrayLists with Struts ActionForms as well. I
was always getting ArrayIndexOutOfBounds exceptions because unlike
regular arrays, Struts does NOT set ArrayLists to the proper size to
accept the incoming indexed form parameters.

Here's my "hack". I can now use ArrayLists in ActionForms without a
problem. Obviously like the poster before me said, you must set the
proper index setters and getters in your ActionForm as well. Instead
of ArrayList use ArrayListCustom (for lack of a better name) to ensure
the list is sized properly during Strut's auto-population process.

public class ArrayListCustom extends ArrayList {

/**
* Overrides parent's set method to ensure that ArrayList
* is always large enough to store the element index being set.
* Did this to overcome issue with Struts Action form population
* mechanism which works fine with regular arrays but not with
ArrayLists.
*
*/
public Object set(int index, Object element){
if(index<this.size()){
return super.set(index,element);
}else{
//add "" elements to ArrayList until it reaches proper size
int num2add= index+1-this.size();
for(int i=0;i<num2add;i++){
this.add("");
}
return super.set(index,element);
}
}

}
 
S

Sudsy

John said:
I had a problem using ArrayLists with Struts ActionForms as well. I
was always getting ArrayIndexOutOfBounds exceptions because unlike
regular arrays, Struts does NOT set ArrayLists to the proper size to
accept the incoming indexed form parameters.

Here's my "hack".
<snip>

Then you obviously didn't read the article I cited. It demonstrates
the same technique albeit in a while loop. You try to save others
some time and trouble by documenting your experiences and where does
it get you? (sigh)
 
A

Andrew Thompson

Then you obviously didn't read the article I cited. It demonstrates
the same technique

I have experienced that feeling..
..You try to save others
some time and trouble by documenting your experiences and where does
it get you? (sigh)

(chuckles) My 'success rate' for today's replies
to my URL's is running about (waggles hand) 50/50.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top