0
我在使用struts 2中的类型转换来转换bean的集合时遇到了麻烦。 我下面的动作类:struts 2集合类型转换问题
@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>
当过程已经提交,支柱总是给我4对象,而不是内部集合中的2个对象。我知道使用索引属性中的其他解决方法将解决问题,但对于我的情况,我需要收集是动态的。有办法解决这类问题吗?
我已经试过别人的注释以及:
@Element(value =foo.HelloBean.class)
@CreateIfNull(value = true)
@KeyProperty(value = "name")
private List<HelloBean> helloBeans = new ArrayList<HelloBean>();
但这些都不曾
仅供参考:您无需在您的操作中初始化helloBeans。你的二传手会做到这一点。 – 2010-12-12 16:45:45