我使用两个标签。它们具有相同的名称,但不同的 ID
<s:doubleselect id="countryId1" name="country" list="countriesMap.keySet()" doubleId="cityId1" doubleName="city" doubleList="countriesMap.get(top)" />
<s:doubleselect id="countryId2" name="country" list="countriesMap.keySet()" doubleId="cityId2" doubleName="city" doubleList="countriesMap.get(top)" />
在行动中我试图得到
country String[] countryArray = ServletRequest.getParameterValues("country");
但我得到 countryArray = null
。我查看了页面代码,发现了这样的情况
<select name="country" id="countryId1" onchange="countryId1Redirect(this.options.selectedIndex)">
<option value="USA">USA</option>
<option value="Germany">Germany</option>
</select>
我选择值USA,但是没有selected='selected'
属性(property)。
如何从每个 <select name...
中放置选定的值进入数组?
最佳答案
要从具有相同名称的元素中获取值列表,请创建与名称匹配的 getter 和 setter;例如:
public class MyAction extends ActionSupport {
private List<String> countries;
private List<String> cities;
public String execute() {
if (getCountry() != null && getCity() != null) {
for (int i = 0; i < getCountry().size(); i++) {
System.out.println("country"+(i+1)+"="+getCountry().get(i));
System.out.println("city"+(i+1)+"="+getCity().get(i));
}
}
return SUCCESS;
}
// setCountry matches country
public void setCountry(List<String> countries) {
this.countries = countries;
}
public List<String> getCountry() {
return countries;
}
// setCity matches city
public void setCity(List<String> cities) {
this.cities = cities;
}
public List<String> getCity() {
return cities;
}
}
我相信你可以使用String[]
而不是List<String>
如果你愿意的话。
我目前没有办法对此进行测试,但您可以使用状态变量的索引属性来获取迭代索引,可能是这样的:
<s:iterator value="country" status="stat">
<s:property /> <!-- the country -->
<br />
<s:property value="#city[#stat.index]" /> <!-- the city corresponding to the current country -->
<br />
<br />
</s:iterator>
关于java - struts2 <s :doubleselect>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10001182/