我觉得这应该非常明显,但到目前为止我找不到答案。
我想要一个字符串列表(或一个字符串数组,我真的不在乎)由Struts2中的表单数据填充。
我已经看过几个如何做indexed properties with beans的例子,但在对象中包装一个字符串似乎相当愚蠢。
所以我有类似
的东西 public class Controller extends ActionSupport {
private List<String> strings = new ArrayList<String>();
public Controller() {
strings.add("1");
strings.add("2");
strings.add("3");
strings.add("4");
strings.add("5");
}
public String execute() throws Exception {
return ActionSupport.SUCCESS;
}
public List<String> getStrings() {
return strings;
}
public void setStrings(List<String> s) {
strings = s;
}
}
...
<s:iterator value="strings" status="stringStatus">
<s:textfield name="strings[%{#stringStatus.index}]" style="width: 5em" />
</s:iterator>
表单字段会填充其初始值(例如1,2等),但结果未正确发布回来。永远不会调用setStrings
,但会将值设置为空字符串。
有人知道发生了什么事吗?提前谢谢!
答案 0 :(得分:7)
我相信你拥有它,你的jsp代码会呈现如下内容:
<input type="text" name="strings[0]" style="width: 5em" value="1"/>
<input type="text" name="strings[1]" style="width: 5em" value="2"/>
<input type="text" name="strings[2]" style="width: 5em" value="3"/>
...
请注意,字段引用的名称是“strings [x]”,因为您需要名称只是“字符串”。我会建议像:
<s:iterator value="strings" status="stringStatus">
<s:textfield name="strings" value="%{[0].toString()}" style="width: 5em" />
</s:iterator>
不确定上面的value属性是否正确,但我认为这样的结果会为您提供所需的结果。