Types struts 2集合类型转换问题

Types struts 2集合类型转换问题,types,struts2,type-conversion,Types,Struts2,Type Conversion,我在struts 2中使用类型转换转换转换bean集合时遇到问题。 我有以下动作课: @Validation() @Conversion() public class HelloWorldAction extends ActionSupport { private List<HelloBean> helloBeans = new ArrayList<HelloBean>(); public String execute() throws Excep

我在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;
    }

}
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>();
@Element(value=foo.HelloBean.class)
@CreateIfNull(值=真)
@KeyProperty(value=“name”)
private List helloBeans=new ArrayList();
但是这些都不起作用,因为您必须使用:

    <s:form action="hello-world">
        <s:textfield name="helloBeans[1].name" label="name1"/>
        <s:textfield name="helloBeans[1].age" label="age1"/>
        <s:textfield name="helloBeans[2].name" label="name2" />
        <s:textfield name="helloBeans[2].age" label="age2"/>
        <s:submit />
    </s:form>

我认为最大的问题不是必须这样做,而是你认为这意味着它不能是动态的,认为文本字段被渲染得更少(实际上是因为我删除了ID和‘值’属性):


... 
没有理由不能像测试视图那样在jsp中动态处理此问题:

    <h1>Display HelloBeans</h1>
    <table>
        <s:iterator value="helloBeans">
            <tr>
                <td><s:property value="name"/></td>
                <td><s:property value="age"/></td>
            </tr>
        </s:iterator>
    </table>
显示HelloBeans
或者,如果您的问题是客户端,那么使用JavaScript(或者更好的JS库,如jQuery)向DOM添加新的文本字段。。。并使用字符串连接为“name”属性构建正确的OGNL

注意,如果您的动态行为是JSP中的服务器端,那么

<s:property value="#helloBeans.index" /> 


当在s:iterator标记中使用时,将为您获取当前迭代的索引。

FYI:您不需要在操作中初始化helloBeans。你的二传手会这样做的。
<s:property value="#helloBeans.index" />