我正试图在Thymeleaf中绑定一个列表,并已按照教程进行搜索并在此处搜索;我在提交时的绑定索引中有一个问题被跳过,然后被超出。首先,我将详细说明代码,核心内容如下:
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;
}
}
然后我们有一个包装器,以便可以绑定列表:
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;
}
}
然后在此之后,我们有了百里香模板,将详细信息绑定到表单:
<!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>
因此,这将很好地显示并显示其中有305个项目。当我单击Submit时,一切正常,直到我达到256个项目为止(注意-256可能是一个线索,但这将是一个字节变量! )。然后我得到一个索引超出范围错误,如下所示:
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
所以看来我可以毫无问题地读出305个项目,但是当我尝试再次添加它们时,我认为Thymeleaf应该执行以下操作:
对于每个属性,请在列表末尾获取StockItem并设置属性
这很好,直到列表中超过256个元素为止。
有人有处理更大列表的例子吗,对发生的事情有任何想法吗?
在此先感谢您的帮助。
干杯
尼尔
答案 0 :(得分:0)
我找到了解决方案,与Thymeleaf无关。 Spring对可以绑定到255的项目设置了限制。如果要提高级别,可以通过添加以下内容来更改此限制:
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setAutoGrowCollectionLimit(600);
}