【问题标题】:Index out of bound in spring MVCSpring MVC中的索引超出范围
【发布时间】:2015-11-05 12:14:58
【问题描述】:

我有一个 bean AgreegateBean,我将其用作传输对象。 bean 的类定义为 -

Class AgreegateBean {

 private SomeOtherBean bean;
 private List<Person> someList;

 // getters and setters
}

我在带有 ModelAttribute 注释的 spring 控制器中使用这个 bean。对于 JSP,我有 JSTL。我已经用这样的字段填充了 JSP。

 <input type="text" name="someList[0].name" />
 <input type="text" name="someList[0].surName" />

当我提交表单时,我收到 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 .

org.springframework.beans.InvalidPropertyException: Invalid property 'someList[0]' of bean class [com.form.bean.AgreegateBean]: Index of out of bounds in property path 'someList[0]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

【问题讨论】:

    标签: java spring jsp spring-mvc


    【解决方案1】:

    这是因为检索到的List&lt;Person&gt; someList;中没有元素

    之前检查一下:

    <c:if test="${someList != null}">
        <input type="text" name="someList[0].name" />
        <input type="text" name="someList[0].surName" />
    </c:if>
    

    【讨论】:

    • 我认为这不是问题,因为 OP 提到了“提交”表单。我认为这更像是 Spring 没有创建 Person 的新实例。
    【解决方案2】:

    找到解决方案。没有在 getter 和 setter 中使用泛型。不知道如何,但添加泛型可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      相关资源
      最近更新 更多