gpt4 book ai didi

java - 分配属性时超出范围的索引。?

转载 作者:行者123 更新时间:2023-11-30 11:12:46 24 4
gpt4 key购买 nike

我有下面的 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 是来自 Controller 的代码,我正在使用 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

请指导我解决这个问题。

最佳答案

应该是:

<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不是 nullfieldValue.textValues不为空
  • 代码有一个条件来检查当前循环的索引(由变量no 表示)是否仍在 ArrayList 的范围内fieldValue.textValues .
  • 上面的代码还假定您已经导入了 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions"
    prefix="fn" %>
    给你的jsp .

关于java - 分配属性时超出范围的索引。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26731219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com