2014-11-04 65 views
2

我有下面的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 

請指導我解決這個問題。

+0

你已經把'private ArrayListList '放在你的代碼中。嘗試'ArrayList '。或者你在代碼片段中有錯字嗎? – ha9u63ar 2014-11-04 09:13:57

+1

在你的控制器中是錯字 – RBP 2014-11-04 09:22:26

+0

,我們可以看到你初始化'fieldValue'的部分嗎? – jcera 2014-11-04 14:42:26

回答

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> 

注意

  • 上面的代碼假定fieldValuenullfieldValue.textValues不是空
  • 代碼有一個條件,以檢查是否索引(用變量no表示)電流回路仍在的界限ArrayListfieldValue.textValues
  • 上面的代碼還假定您已經將<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>導入到jsp
+0

$ {fields}是另一個列表對象。我正在迭代它,並在每次迭代中創建一個輸入字段。我想用filedValue arraylist綁定輸入字段。 – RBP 2014-11-04 09:27:58

+0

@RBP'$ {fields}'是什麼的列表? 'FieldValue'對象? – jcera 2014-11-04 09:30:56

+0

$ {fields}是字段對象的列表,而不是FieldValue。 – RBP 2014-11-04 09:34:44