【问题标题】:How to get updated list values from JSP in my action?如何在我的操作中从 JSP 获取更新的列表值?
【发布时间】:2013-10-09 21:23:57
【问题描述】:

我有一个 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 的值时,我应该在我的操作中获得更新后的值。但我不能吗。有什么方法可以获取这些更新的值。

【问题讨论】:

  • struts 使用 %{variableName} 吗?
  • 是的...它支持 OGNL 表达式
  • 所以不应该是 ${variableName}
  • 应该是....但是 %{variableName} 也可以工作..我在 jsp 中获取值..但是当我更新值并提交表单时,我没有得到更新的值在行动中

标签: java jsp struts2 annotations type-conversion


【解决方案1】:

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

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

【讨论】:

  • 它有效!!!谢谢罗马人...非常感谢...挽救了我的一天。学习是件好事
  • 太好了,现在您必须编写代码来比较列表和更新值。但是您没有保存 previuos 列表,如果您不介意在操作中对值进行硬编码,则需要再次获取它。最好将它放在会话中,因为每次发出请求时都会重新创建操作。
猜你喜欢
  • 2020-07-10
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多