【发布时间】:2010-12-12 03:59:57
【问题描述】:
我在 struts 2 中使用类型转换来转换 bean 的集合时遇到了麻烦。 我有以下动作类:
@Validation()
@Conversion()
public class HelloWorldAction extends ActionSupport {
private List<HelloBean> helloBeans = new ArrayList<HelloBean>();
public String execute() throws Exception {
System.out.println(helloBeans);
return SUCCESS;
}
public List<HelloBean> getHelloBeans() {
return helloBeans;
}
@TypeConversion(rule = ConversionRule.COLLECTION, converter = "foo.HelloBean")
public void setHelloBeans(List<HelloBean> helloBeans) {
this.helloBeans = helloBeans;
}
}
还有我的 bean 类:
public class HelloBean {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
还有我的 JSP 文件:
<s:form action="helloWorld">
<s:textfield name="helloBeans.name" label="name1"/>
<s:textfield name="helloBeans.name" label="name2" />
<s:textfield name="helloBeans.age" label="age1"/>
<s:textfield name="helloBeans.age" label="age2"/>
<s:submit />
</s:form>
提交过程后,struts 总是给我 4 个对象,而不是集合内的 2 个对象。我知道在属性中使用索引的其他解决方法可以解决问题,但就我而言,我需要动态收集。有没有办法解决这种问题?
我也尝试过其他注释:
@Element(value =foo.HelloBean.class )
@CreateIfNull( value = true )
@KeyProperty( value = "name" )
private List<HelloBean> helloBeans = new ArrayList<HelloBean>();
但这些都不起作用
【问题讨论】:
-
仅供参考:您不需要在操作中初始化 helloBeans。你的二传手会这样做。
标签: types struts2 type-conversion