1

I have the Person & Fruit bean defined like this:

public class Person {
    private String fullName;
    private List<Fruit> favoriteFruits;
    //getters setters and constructors here.
    ...
}

public class Fruit {
    private String name;
    //getter setter and constructors here.
}

Then I have a Struts2 Action like this:

public class AddPersonAction extends ActionSupport {
    private static final long serialVersionUID = -1856680463263215989L;

    private Person person;

    @Override
    public String execute() throws Exception {      
        if (person!= null) {            
            //TODO add person to database.          
            return SUCCESS;
        } else {            
            return INPUT;
        }
    }    
}

And the Strus2 html form looks like this:

...
<form...>
  <s:textfield name="person.fullName"/>

  <!--This is where I would allow user to create one or more textboxes using javascript. 
    And allow them to input multiple favorite fruits -->

  <s:submit name="submit" value="Submit" />
</form>
...

Now, my question, the way I have mapped textfield named person.fullName directly to the Person bean's fullName property, how can I map the multiple textboxes (favorite fruit) to the Person bean's favoriteFruits collection (List<Fruit>) ?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Vijay Pant
  • 74
  • 1
  • 7

1 Answers1

0

To send back a Collection instead of a single value, use an index.

<s:textfield value="person.favoriteFruits[0].name" />
<s:textfield value="person.favoriteFruits[1].name" />
...

To create it with Javascript, follow this question and answer and simply do the math.

Also remember to always specify a no-args constructor (if creating a constructor yourself, otherwise it's not necessary since it's there by default) to allow Struts2 map correctly

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243