How to populate a ArrayList FormBean attribute?

Discussion in 'Java' started by Eduardo, Aug 30, 2004.

  1. Eduardo

    Eduardo Guest

    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
     
    Eduardo, Aug 30, 2004
    #1
    1. Advertisements

  2. Eduardo

    Sudsy Guest

    Eduardo wrote:
    <snip>
    > 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>
     
    Sudsy, Aug 30, 2004
    #2
    1. Advertisements

  3. Eduardo

    John Raab Guest

    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);
    }
    }

    }




    Sudsy <> wrote in message news:<>...
    > Eduardo wrote:
    > <snip>
    > > 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>
     
    John Raab, Sep 19, 2004
    #3
  4. Eduardo

    Sudsy Guest

    John Raab wrote:
    > 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)
     
    Sudsy, Sep 19, 2004
    #4
  5. On Sat, 18 Sep 2004 21:33:03 -0400, Sudsy wrote:

    > 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
     
    Andrew Thompson, Sep 19, 2004
    #5
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,059
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    3,080
    Kaidi
    Jan 3, 2004
  3. Uwe Voigt
    Replies:
    5
    Views:
    13,257
  4. gbattine

    Populate an arraylist....

    gbattine, Jun 12, 2006, in forum: Java
    Replies:
    4
    Views:
    8,373
    Oliver Wong
    Jun 13, 2006
  5. Squishy
    Replies:
    1
    Views:
    12,546
  6. xz
    Replies:
    16
    Views:
    2,725
  7. Philipp
    Replies:
    6
    Views:
    1,259
    Arne Vajhøj
    May 28, 2008
  8. Enjoy Life
    Replies:
    2
    Views:
    491
    Roland Hall
    Feb 23, 2005
Loading...