java - Thymeleaf 中的绑定列表;索引超出范围
问题描述
我正在尝试在 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;
}
}
然后在此之后,我们有 thymeleaf 模板将详细信息绑定到表单:
<!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 个项目。当我点击提交时,我让一切正常,直到我点击 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 对象
- 从 StockItems 对象中获取列表
- 将该 StockItem 添加到列表的末尾
对于每个属性,获取列表末尾的 StockItem 并设置属性
这工作正常,直到我们超过列表中的 256 个元素。
有没有人有处理更大列表的示例并且对正在发生的事情有任何想法?
在此先感谢您的帮助。
干杯,
尼尔
解决方案
我找到了解决方案,它与 Thymeleaf 无关。Spring 对可以绑定到 255 的项目数设置了限制。如果您想通过添加更高的值来更改此设置:
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setAutoGrowCollectionLimit(600);
}
推荐阅读
- r - 如何使用 anova() 在虚拟交互中找到主效应的总体意义
- c# - dotnet 核心配置绑定不起作用
- java - Spring Boot 自定义启动器 - 无法导入自定义启动器类
- list - Flutter List、详情页和待办事项
- jquery - 数据表只计算特定列中的行数
- ios - 在 OS X 上安装 OpenSSL 时出现架构不支持错误
- javascript - Angular Reactive Forms 动态嵌套数组检测失去焦点时的变化
- python - 如何获取 Dataframe 中前一行的值以更改当前行值(动态,而不是静态值)
- python - Google Speech to Text - 无法将输出写入文本文件
- sql-server - 带有 null 的 SQL Server 案例语句