user1006080
user1006080

Reputation: 75

a list of list value populated in struts2 iterator

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?

Upvotes: 0

Views: 5268

Answers (2)

Dave Newton
Dave Newton

Reputation: 160301

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.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160301

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

Upvotes: 1

Related Questions