![](/img/trans.png)
[英]Sping MVC, Thymeleaf, POST request, how to pass list of objects to controller
[英]Binding embedded List of objects on a Thymeleaf template for a POST request
我正在开发杂货店列表Web应用程序。 一种功能应该是(取消)检查列表中找到/购买的物品。 显示所有值都可以正常工作。 但是,当我在一些购买的商品上选中某些复选框并尝试过@ModelAttribute
后,spring MVC控制器中传入的@ModelAttribute
为null(id除外)。
我希望你能帮助我。
这是我的课程:
public class PurchaseList {
private Long id;
private List<PurchaseItem> purchaseItemList;
private DateTime purchaseDate;
private boolean done;
}
public class PurchaseItem {
private Long id;
private String purchaseItemName;
private PurchaseCategory purchaseCategory;
private PurchaseList purchaseList;
private boolean found;
}
控制器:
@RequestMapping(value="/{id}", method=RequestMethod.POST)
public String postPurchaseList(@PathVariable(value="id") Long id, @ModelAttribute("purchaseList") PurchaseList purchaseList, Model model) {
List<PurchaseList> notDonePurchaseList = purchaseListService.getNotDonePurchaseList();
model.addAttribute("notDonePurchases", notDonePurchaseList);
purchaseListService.savePurchaseList(purchaseList);
return "purchaseList";
}
胸腺模板
<form action="#" th:action="@{/purchaseList/{purchaseListId}/(purchaseListId=${purchaseList.id})}" th:object="${purchaseList}" method="post">
<table class="table table-bordered table-hover">
<thead>
<tr>
<td>Found</td>
<td>Item</td>
<td>Category</td>
</tr>
</thead>
<tr th:each="item,status : ${purchaseList.purchaseItemList}">
<td><input type="checkbox" th:checked="${item.found}" th:value="*{purchaseItemList[__${status.index}__].found}" /></td>
<td th:text="${item.purchaseItemName}"></td>
<td th:text="${item.purchaseCategory.categoryName}"></td>
</tr>
</table>
<input type="submit" value="Submit" id="submit" />
</form>
我知道我没有设置其他值(仅是found
属性)。 还是这个问题?
最好的祝福
编辑:
ArrayList
。 现在,空的ArrayList
返回给控制器。 在PurchaseList类中初始化PurchaseItemList。 public class PurchaseList { private Long id; private List<PurchaseItem> purchaseItemList=new ArrayList<>(); private DateTime purchaseDate; private boolean done; }
给输入字段一个像这样的ID。
<td><input type="checkbox" th:checked="${item.found}" th:value="* {purchaseItemList[__${status.index}__].found}" th:id='purchaseList.purchaseItemList[__${status.index}__].found'/></td>
我找到了问题和解决方案。
我已经在一些教程和博客文章中看到了th:field=*{..}
的用法,但是我没有使用它,因为我信任Spring Tool Suite(它在自动完成功能上不提供th:field
属性)因此,我确定无法再使用此属性,但是我错了,我在模板代码中将某些*th:value=${..}
属性更改为th:field=*{..}
如下:
<form action="#" th:action="@{/purchaseList/{purchaseListId}/(purchaseListId=${purchaseList.id})}" th:object="${purchaseList}" method="post">
<input type="hidden" th:field="*{id}" />
<input type="hidden" th:field="*{purchaseDate}" />
<input type="hidden" th:field="*{done}" />
<table class="table table-bordered table-hover">
<thead>
<tr>
<td>Found</td>
<td>Item</td>
<td>Category</td>
</tr>
</thead>
<tr th:each="item,status : ${purchaseList.purchaseItemList}">
<td><input type="checkbox" th:checked="${item.found}" th:field="*{purchaseItemList[__${status.index}__].found}" th:id="${'purchaseList.purchaseItemList[__${status.index}__].found' + 'status.index'}"/></td>
<td th:text="${purchaseList.purchaseItemList[__${status.index}__].purchaseItemName}">
<input type="hidden" th:field="*{purchaseItemList[__${status.index}__].purchaseItemName}"
th:value="${purchaseList.purchaseItemList[__${status.index}__].purchaseItemName}"
th:id="${'purchaseList.purchaseItemList[__${status.index}__].purchaseItemName'}"
/>
</td>
<td th:text="${item.purchaseCategory.categoryName}">
<input type="hidden" th:field="*{purchaseItemList[__${status.index}__].purchaseCategory.categoryName}"
th:value="${purchaseList.purchaseItemList[__${status.index}__].purchaseCategory.categoryName}"
th:id="${'purchaseList.purchaseItemList[__${status.index}__].purchaseCategory.categoryName'}"
/>
</td>
</tr>
</table>
<input type="submit" value="Submit" id="submit" />
</form>
现在效果更好。 我在控制器中获得“找到的”属性。 其他值未设置。 但是我想我在隐藏的输入字段中弄乱了一些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.