0

我正在使用 WebSphere 5.1.2 并认为它是 Struts 1,但我不确定。

我的问题如下。

我的表单中有一个 bean BeanName 的动态列表,输入类型为文本。

我的表格是这样的(无法复制和粘贴,因为它在没有互联网的虚拟机上):

public class MyForm extends ActionForm implements Serializable {
    // Property
    private List beanNameList = new ArrayList();

    // Simple Getter
    public List getBeanNameList() {
        if (beanNameList == null) {
            beanNameList = new ArrayList();
        }
        return beanNameList ;
    }

    // Item Getter
    public BeanName getBeanNameList(int index) {
        if (beanNameList == null) {
            beanNameList = new ArrayList();
        }
        for (int i = beanNameList.size(); i <= index; i++) {
            beanNameList.add(new BeanName());
        }
        return (BeanName)contractList.get(index);
    }

    // Simple Setter
    public List setBeanNameList(List value) {
        return beanNameList = value;
    }

    // Item Setter
    public BeanName getBeanNameList(int index, BeanName value) {
        if (beanNameList == null) {
            beanNameList = new ArrayList();
        }
        for (int i = beanNameList.size(); i <= index; i++) {
            beanNameList.add(new BeanName());
        }
        contractList.set(index, value);
    }
}

当我提交表单时,我收到一个 IndexOutOfBoundsException: index: 3, size: 0。

分析控制台,我意识到 Struts 使用的是 ArrayList.get,而不是 getBeanNameList,如图所示:

控制台错误

有任何想法吗?

4

1 回答 1

1

我发现了问题...

我的 Struts 版本太旧了,它真的不使用带有索引的 getter 和 setter,它使用的是 ArrayList.get 或类似的东西。

为了解决这个问题,我用列表的大小创建了一个隐藏字段,在表单的重置方法上,我只是设置了列表的大小。

于 2016-08-06T05:28:29.797 回答