1

我有一个 JSP,我在其中迭代一个列表并创建一个可编辑的表。

<s:iterator value="test" id="countryList">
  <tr>
    <td><s:textfield name="countryCode" value="%{countryCode}" theme="simple" /></td>
    <td><s:textfield name="countryName" value="%{countryName}" theme="simple" /></td>
  </tr>
 </s:iterator>

我的列表:

List<Country> test = new ArrayList<Country>();  

国家级:

public class Country {
    private String countryCode;
    private String countryName;


and the getter setters......

并说我像这样填充列表:

Country country = new Country();
Country country1 = new Country();

country.setCountryCode("1");
country.setCountryName("India");

country1.setCountryCode("2");
country1.setCountryName("US");

test.add(country);
test.add(country1);

现在,当我在 JSP 中更改 和 的值时,我应该在我的操作中获得更新的值countrycodecountryname但我不能吗。有什么方法可以获取这些更新的值。

4

1 回答 1

2

您需要在拦截器使用索引属性访问填充您的列表Country时创建对象。params

<s:iterator value="test" id="countryList" status="stat">
  <tr>
    <td><s:textfield name="countryList[%{#stat.index}].countryCode" value="%{countryCode}" theme="simple" /></td>
    <td><s:textfield name="countryList[%{#stat.index}].countryName" value="%{countryName}" theme="simple" /></td>
  </tr>
 </s:iterator>

要让 struts 填充列表,您应该在 xml 配置中使用类型转换注释或等效项。

@Element(value = Country.class)
@Key(value = String.class)
@KeyProperty(value = "countryCode") 
@CreateIfNull(value = true)
private List<Country> countryList = new ArrayList<Country>;
于 2013-10-03T09:21:37.530 回答