Nei*_*enn 3 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.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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我们有一个包装器,以便可以绑定列表:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我们使用 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>
Run Code Online (Sandbox Code Playgroud)
因此,这显示得很好,并显示了 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
Run Code Online (Sandbox Code Playgroud)
所以看起来我可以毫无问题地读出 305 个项目,但是当我尝试再次将它们添加回来时,我认为 Thymeleaf 应该执行以下操作:
对于每个属性,获取列表末尾的 StockItem 并设置属性
在列表中的元素超过 256 个之前,此方法工作正常。
有谁有处理更大列表的示例并且知道发生了什么?
在此先感谢您的帮助。
干杯,
尼尔
我找到了解决方案,与 Thymeleaf 无关。Spring 设置了可以绑定到 255 的项目数限制。如果您想更高,则需要通过添加以下内容来更改此设置:
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setAutoGrowCollectionLimit(600);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1432 次 |
| 最近记录: |