【问题标题】:Index of out of bounds while assigning property .?分配属性时超出范围的索引。?
【发布时间】:2014-11-04 09:05:49
【问题描述】:

我有下面的 FieldValue 模态类,带有 String 的 ArrayList 的 textValues 属性。

@Entity
@Table(name = "TBL_STD_FIELD_VALUE")
public class FieldValue implements Serializable {

@Id
@GeneratedValue
@Column(name = "FLD_VERSION")
private int version;

@Column(name = "FLD_VALUE")
private ArrayList<String> textValues;

  public ArrayList<String> getTextValues() {
    if(this.textValues == null){
        return new ArrayList<String>();
    }
   return textValues;
  }
}

下面是 addForm.jsp 。我正在将该 textValues 列表分配给输入字段,如下面的代码所示。

<form:form name="moduleForm" modelAttribute="fieldValue"  id="moduleForm"   action="/module/saveAddForm.htm" method="POST" onsubmit="return validateMandetoryFields();">
    <table width="100%" id="preferenceTable" style="border: 0px solid #ccc;">
        <c:forEach items="${fields}" var="field" varStatus="no">
            <tr height="30px"  bordercolor="#FFF" style="cursor: pointer;" title="Module Name">
                <td width="60%" align="left">
                    <form:input type="${field.type}" style="width: 250px;" path="textValues[${no.index}]"   title="Name of the Module" />  
                </td>
         </tr>
      </c:forEach>

Belos 是来自控制器的代码,我正在使用 MultiActionController。

public ModelAndView addForm(HttpServletRequest request,HttpServletResponse response) {
    String moduleId = request.getParameter("moduleId");

    Module module = moduleDao.get(Module.class,Long.parseLong(moduleId));
    List<Fields> fields = fieldDao.getAllFields(Long.parseLong(moduleId));

    ModelAndView model = new  ModelAndView("admin/module/addForm");
    model.addObject("fields",fields);
    model.addObject("fieldValue",fieldValue);

    return model;
} 

它给了我下面的错误信息。

org.springframework.beans.InvalidPropertyException: Invalid property 'textValues[0]' of bean class [com.cloudcodes.gdirectory.model.module.FieldValue]: Index of out of bounds in property path 'textValues[0]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

请指导我解决此问题。

【问题讨论】:

  • 您已将private ArrayListList&lt;String&gt; 放入您的代码中。试试ArrayList&lt;String&gt;。还是您的代码 sn-p 有错字?
  • 在你的控制器中,我们能看到你初始化fieldValue的部分吗?
  • FieldValue fieldValue = new FieldValue();这条线在控制器中。

标签: java jstl taglib


【解决方案1】:

应该是:

<c:choose>
    <c:when test="${!empty fieldValue.textValues && no.index < fn:length(fieldValue.textValues)}">
        <form:input type="${field.type}" 
            style="width: 250px;" 
            path="${fieldValue.textValues[${no.index}]}"   
            title="Name of the Module" />
    </c:when>
    <c:otherwise>
        <!-- for index that is beyond the length of fieldValue.textValues -->
        <form:input type="${field.type}" 
            style="width: 250px;" 
            path=""   
            title="Name of the Module" />
    </c:otherwise>
</c:choose>

注意

  • 上面的代码假定fieldValue 不是null 并且fieldValue.textValues 不是空的
  • 代码有条件检查当前循环的索引(由变量no表示)是否仍然ArrayList@的范围内987654327@.
  • 上面的代码还假设您已经将<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 导入到您的jsp

【讨论】:

  • ${fields} 是另一个列表对象。我正在迭代它,并在每次迭代中创建一个输入文件。我想将该输入字段与 filedValue 数组列表绑定。
  • @RBP ${fields} 是什么列表? FieldValue 对象?
  • ${fields} 是字段对象的列表,而不是 FieldValue。
  • 我添加了相同的逻辑。我将 FieldValue 设为 null。您能指导我了解它是如何变为 null 的吗?
猜你喜欢
  • 2013-05-31
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-21
  • 2019-06-18
  • 2020-02-13
相关资源
最近更新 更多