0

For my case, it is a little bit trickier. I just have another list in person object, it is called state. like below:

public class People {
    private int id;
    private String name;
    private List<State> states;
    // Plus setters/getters
}

public class State {
    private int id;
private String stateAbbr;
private String stateName;
public State (String stateAbbr, String stateName) {
this.stateAbbr = stateAbbr;
this.stateName = stateName;
}
   // Plus setters/getters
}

Action class:

public class PersonAction extends ActionSupport {
private List<People> peopleList;

public List<People> getPeopleList() {
    return peopleList;
}

public void setPeopleList(List<People> peopleList) {
    this.peopleList = peopleList;
}

//Initial Load method
@Override
public String execute() {
    peopleList = new ArrayList<People>();

    int alpha = 65;
    for(int i = 0; i < 3 ; i++) {
        People people = new People();
        people.setId(i);
        people.setName(String.valueOf((char)alpha++));
        peopleList.add(people);
    }

    for (People people : peopleList){
        State state = new State("BC", "BritishColumbia");
        List<State> states = new ArrayList<State>();
        states.add(state);
        state = new State("AC", "AppleColumbia");
        states.add(state);
        people.setStates(states);
    }
    return SUCCESS;
}

//Function that handles the form submit
public String updatePerson() {
    for(People people : peopleList) {
        System.out.println(people.getId() + ":" + people.getName());
    }

    return SUCCESS;
}

}

JSP page

<s:form action="doUpdate">
    <s:iterator value="peopleList" status="stat" var="people">
        <s:textfield value="%{#people.name}"
            name="peopleList[%{#stat.index}].name" />
        <s:iterator value="states" status="stateStatus" var="personState">
            <s:textfield value="%{#personState.stateAbbr}"
                name="peopleList[%{#stat.index}].states[%{#stateStatus.index}].stateAbbr" label="Abbr State" />
            <br />
        </s:iterator>
    </s:iterator>
    <s:submit value="Submit" />
</s:form>

When I submit this page, I got states is [null] in person, why?

user1006080
  • 75
  • 1
  • 4
  • 11

2 Answers2

1

Answer for the new question:

If your State class doesn't have a default constructor, S2/OGNL will not be able to instantiate an empty version of the class.

Provide a default (no-argument) constructor, and the person's states list will be populated.

(Too bad both answers can't be accepted ;)

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • You are right, after I add no argu construtor, it works. Did this mentioned in any online struts 2 tutorial? why People entity bean doesn't need no argu construtor? and why my states with argu constructor can NOT let S2/OGNL to instantiate state class? – user1006080 Oct 26 '11 at 14:44
  • @user1006080 Please [accept this answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) so others with similar problems know the solution (and, IMO, vote them both up to reward my effort). The `People` class *has* a default constructor; every class *that doesn't define a non-default constructor* has one. Because you defined a non-default ctor in `State`, it no longer had a default (no-arg) ctor. – Dave Newton Oct 26 '11 at 14:47
  • if i use the latest struts 2 version 2.2.3, can I remove # character? – user1006080 Oct 26 '11 at 15:05
  • @user1006080 It looks like I was wrong about that; the rules for when you need a `#` and when you don't are a bit fuzzy. – Dave Newton Oct 26 '11 at 15:09
0

The state property is a property of a specific person. In this case, you need to "connect" the state to persons[%{#stat.index}], so:

<s:textfield ... 
    name="persons[%{#stat.index}].states[%{#stateStatus.index}]" .../>

The iterator status variable has an index property that avoids the math you're doing on the IteratorStatus (docs) instance:

<s:textfield ... name="persons[%{#stat.index}].name" />

Also, depending on which version of S2 you're using, you may not need the # character.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302