struts2 迭代器中填充的列表值的列表

发布于 2024-12-12 02:40:55 字数 2292 浏览 0 评论 0原文

就我而言,这有点棘手。我只是在 person 对象中有另一个列表,它称为状态。如下:

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>

当我提交这个页面时,我得到的状态是[null],为什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哎呦我呸! 2024-12-19 02:40:55

回答新问题:

如果您的 State 类没有默认构造函数,S2/OGNL 将无法实例化该类的空版本。

提供默认(无参数)构造函数,然后将填充人员的状态列表。

(太糟糕了,两个答案都不能被接受;)

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

北音执念 2024-12-19 02:40:55

state 属性是特定人的属性。在这种情况下,您需要将状态“连接”到 persons[%{#stat.index}],因此:

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

迭代器状态变量有一个 index 属性,可以避免您在 IteratorStatus (docs) 实例:

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

此外,根据您使用的 S2 版本,您可能不需要 #字符。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文