在struts2迭代器中填充的列表值的列表 [英] a list of list value populated in struts2 iterator
本文介绍了在struts2迭代器中填充的列表值的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
就我而言,这有点棘手.我在人对象中只有另一个列表,它称为状态.如下所示:
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
}
动作类:
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>
提交此页面时,我本人说是[null],为什么?
When I submit this page, I got states is [null] in person, why?
推荐答案
state
属性是特定人的属性.在这种情况下,您需要将状态连接"到persons[%{#stat.index}]
,所以:
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" />
此外,根据您所使用的S2版本,您可能不需要 #
字符.
Also, depending on which version of S2 you're using, you may not need the #
character.
这篇关于在struts2迭代器中填充的列表值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文