【问题标题】:a list of list value populated in struts2 iteratorstruts2迭代器中填充的列表值列表
【发布时间】:2011-10-25 23:25:49
【问题描述】:

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

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
}

动作类:

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 页面

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

当我提交此页面时,我本人收到 states is [null],为什么?

【问题讨论】:

    标签: list struts2


    【解决方案1】:

    新问题的答案:

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

    提供一个默认(无参数)构造函数,将填充此人的状态列表。

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

    【讨论】:

    • 你是对的,在我添加 no argu construtor 之后,它可以工作。这在任何在线 struts 2 教程中是否提到过?为什么 People 实体 bean 不需要 argu 构造函数?为什么我的带有 argu 构造函数的状态不能让 S2/OGNL 实例化状态类?
    • @user1006080 请accept this answer 让其他有类似问题的人知道解决方案(并且,IMO,投票给他们两个以奖励我的努力)。 People一个默认构造函数; 没有定义非默认构造函数的每个类都有一个。因为您在 State 中定义了非默认 ctor,所以它不再有默认(无参数)ctor。
    • 如果我使用最新的 struts 2 版本 2.2.3,我可以删除 # 字符吗?
    • @user1006080 看来我错了;何时需要# 以及何时不需要的规则有点模糊。
    【解决方案2】:

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

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

    迭代器状态变量有一个 index 属性,可以避免您在 IteratorStatus (docs) 实例上进行的数学运算:

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

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

    【讨论】:

    • @user1006080 我真的看不懂,但请检查提交中发送的内容。
    • 我又重新发了一遍问题,你能再看一下吗?
    • @user1006080 是的。您使用的是什么版本的 S2?
    • @user1006080 顺便说一句,您的People/persons 命名约定是倒退的;类应该是Person,单数,集合应该是people,复数:)
    • 我使用的Struts 2 版本是struts2-core-2.2.1.jar,对吗?我应该尝试最新版本 2.2.3 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 2013-11-01
    • 1970-01-01
    • 2012-10-11
    • 2021-01-04
    • 2020-03-30
    • 1970-01-01
    相关资源
    最近更新 更多