Java 百里香中的绑定列表;索引越界
我正试图在Thymeleaf中绑定一个列表,并按照教程进行了搜索;我有一个问题,提交时绑定的索引被跳过,然后被超过。首先我将详细介绍代码,核心项目如下:Java 百里香中的绑定列表;索引越界,java,spring,thymeleaf,Java,Spring,Thymeleaf,我正试图在Thymeleaf中绑定一个列表,并按照教程进行了搜索;我有一个问题,提交时绑定的索引被跳过,然后被超过。首先我将详细介绍代码,核心项目如下: package com.ziath.manu.stockcheck.model; import java.util.UUID; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Generatio
package com.ziath.manu.stockcheck.model;
import java.util.UUID;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.apache.commons.lang3.builder.ToStringBuilder;
@Entity
public class StockItem {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
private String itemId;
private String description;
private Integer currentStockLevel;
private Integer warnStockLevel;
private Integer errorStockLevel;
private Boolean purchaseOrderPlaced;
public StockItem() {
super();
warnStockLevel = 0;
errorStockLevel = 0;
}
public StockItem(String itemId, String description, Integer stockLevel) {
this();
this.itemId = itemId;
this.description = description;
this.currentStockLevel = stockLevel;
}
public String getItemId() {
return itemId;
}
public void setItemId(String id) {
this.itemId = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getCurrentStockLevel() {
return currentStockLevel;
}
public void setCurrentStockLevel(Integer stockLevel) {
this.currentStockLevel = stockLevel;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
public Integer getWarnStockLevel() {
return warnStockLevel;
}
public void setWarnStockLevel(Integer warnStockLevel) {
this.warnStockLevel = warnStockLevel;
}
public Integer getErrorStockLevel() {
return errorStockLevel;
}
public void setErrorStockLevel(Integer errorStockLevel) {
this.errorStockLevel = errorStockLevel;
}
public Boolean getPurchaseOrderPlaced() {
return purchaseOrderPlaced;
}
public void setPurchaseOrderPlaced(Boolean purchaseOrderPlaced) {
this.purchaseOrderPlaced = purchaseOrderPlaced;
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
}
stock items size 256
com.ziath.manu.stockcheck.model.StockItem@50fa1f72[id=b9019869-0e10-4d24-bddd-173fb75f6570,itemId=Washer M3 Silver,description=Washer M3 Silver,currentStockLevel=26,warnStockLevel=0,errorStockLevel=0,purchaseOrderPlaced=<null>]
2019-02-01 12:54:55.509 ERROR 28408 --- [nio-8084-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'stockItems[256]' of bean class [com.ziath.manu.stockcheck.model.StockItems]: Index of out of bounds in property path 'stockItems[256]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256] with root cause
然后我们有一个包装器,以便可以绑定列表:
package com.ziath.manu.stockcheck.model;
import java.util.ArrayList;
import java.util.List;
public class StockItems {
private List<StockItem> stockItems = new ArrayList<>();
public List<StockItem> getStockItems() {
return stockItems;
}
public void setStockItems(List<StockItem> stockItems) {
this.stockItems = stockItems;
}
}
package com.ziath.manu.stockcheck.model;
导入java.util.ArrayList;
导入java.util.List;
公共类库存物品{
private List stockItems=new ArrayList();
公共列表getStockItems(){
归还库存物品;
}
公共作废集合库存项目(列出库存项目){
this.stockItems=stockItems;
}
}
然后,我们有了ThymileAF模板,将详细信息绑定到表单:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Stock Level from Manu</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="#" th:object="${formItems}" th:action="@{/saveStockLevelAlerts}" method="post">
<table>
<tr th:each="stockItem, itemStat : *{stockItems}">
<td th:text="${__${itemStat.index}__}" />
<td th:text="${stockItem.itemId}" />
<td th:text="${stockItem.description}" />
<td th:text="${stockItem.currentStockLevel}" />
<!-- if you want to know what this is go to https://www.baeldung.com/thymeleaf-list -->
<input type="hidden" th:field="${formItems.stockItems[__${itemStat.index}__].id}" th:value="${stockItem.id}">
<input type="hidden" th:field="${formItems.stockItems[__${itemStat.index}__].itemId}" th:value="${stockItem.itemId}">
<input type="hidden" th:field="${formItems.stockItems[__${itemStat.index}__].description}" th:value="${stockItem.description}">
<input type="hidden" th:field="${formItems.stockItems[__${itemStat.index}__].currentStockLevel}" th:value="${stockItem.currentStockLevel}">
<td><input th:field="${formItems.stockItems[__${itemStat.index}__].warnStockLevel}" th:value="${stockItem.warnStockLevel}"></td>
<td><input th:field="${formItems.stockItems[__${itemStat.index}__].errorStockLevel}" th:value="${stockItem.errorStockLevel}"></td>
</tr>
</table>
<input type="submit" id="submitButton" th:value="Save">
<input type="reset" id="resetButton" name="cancel" th:value="Cancel"/>
</form>
</body>
</html>
Manu的库存水平
因此,它显示得很好,并显示有305个项目的项目。当我点击submit时,我会让一切正常工作,直到我点击256项(注意-256可能是一个线索,但那将是一个字节变量!)。然后我得到一个索引越界错误,如下所示:
package com.ziath.manu.stockcheck.model;
import java.util.UUID;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.apache.commons.lang3.builder.ToStringBuilder;
@Entity
public class StockItem {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
private String itemId;
private String description;
private Integer currentStockLevel;
private Integer warnStockLevel;
private Integer errorStockLevel;
private Boolean purchaseOrderPlaced;
public StockItem() {
super();
warnStockLevel = 0;
errorStockLevel = 0;
}
public StockItem(String itemId, String description, Integer stockLevel) {
this();
this.itemId = itemId;
this.description = description;
this.currentStockLevel = stockLevel;
}
public String getItemId() {
return itemId;
}
public void setItemId(String id) {
this.itemId = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getCurrentStockLevel() {
return currentStockLevel;
}
public void setCurrentStockLevel(Integer stockLevel) {
this.currentStockLevel = stockLevel;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
public Integer getWarnStockLevel() {
return warnStockLevel;
}
public void setWarnStockLevel(Integer warnStockLevel) {
this.warnStockLevel = warnStockLevel;
}
public Integer getErrorStockLevel() {
return errorStockLevel;
}
public void setErrorStockLevel(Integer errorStockLevel) {
this.errorStockLevel = errorStockLevel;
}
public Boolean getPurchaseOrderPlaced() {
return purchaseOrderPlaced;
}
public void setPurchaseOrderPlaced(Boolean purchaseOrderPlaced) {
this.purchaseOrderPlaced = purchaseOrderPlaced;
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
}
stock items size 256
com.ziath.manu.stockcheck.model.StockItem@50fa1f72[id=b9019869-0e10-4d24-bddd-173fb75f6570,itemId=Washer M3 Silver,description=Washer M3 Silver,currentStockLevel=26,warnStockLevel=0,errorStockLevel=0,purchaseOrderPlaced=<null>]
2019-02-01 12:54:55.509 ERROR 28408 --- [nio-8084-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'stockItems[256]' of bean class [com.ziath.manu.stockcheck.model.StockItems]: Index of out of bounds in property path 'stockItems[256]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256] with root cause
库存项目大小256
com.ziath.manu.stockcheck.model。StockItem@50fa1f72[id=b9019869-0e10-4d24-bddd-173fb75f6570,itemId=洗衣机M3银色,描述=洗衣机M3银色,currentStockLevel=26,warnStockLevel=0,errorStockLevel=0,purchaseOrderPlaced=]
2019-02-01 12:54:55.509错误28408---[nio-8084-exec-8]o.a.c.c.c.[/].[dispatcherServlet]:路径为[]的上下文中Servlet[dispatcherServlet]的Servlet.service()引发异常[请求处理失败;嵌套异常为org.springframework.beans.InvalidPropertyException:bean类的无效属性“stockItems[256]”[com.ziath.manu.stockcheck.model.StockItems]:属性路径“StockItems[256]”中超出边界的索引;嵌套异常为java.lang.IndexOutOfBoundsException:索引:256,大小:256],带根本原因
因此,我似乎可以毫无疑问地读出305个项目,但当我再次尝试添加它们时,我假设Thymeleaf应该执行以下操作:
Neil我找到了解决方案,与Thymeleaf无关。Spring设置了可绑定到255的项目数限制。如果您想通过添加以下内容来提高项目数,则需要更改此限制:
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setAutoGrowCollectionLimit(600);
}