struts2 迭代器中填充的列表值的列表
就我而言,这有点棘手。我只是在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回答新问题:
如果您的 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 ;)
state
属性是特定人的属性。在这种情况下,您需要将状态“连接”到persons[%{#stat.index}]
,因此:迭代器状态变量有一个
index
属性,可以避免您在IteratorStatus
(docs) 实例:此外,根据您使用的 S2 版本,您可能不需要#字符。
The
state
property is a property of a specific person. In this case, you need to "connect" the state topersons[%{#stat.index}]
, so:The iterator status variable has an
index
property that avoids the math you're doing on theIteratorStatus
(docs) instance:Also, depending on which version of S2 you're using, you may not need the#
character.