java - Binding embedded List of objects on a Thymeleaf template for a POST request -
i developing grocery store list web application. 1 feature should (un)checking found/bought items on list. displaying values works fine. when check checkboxes on bought items , try post afterwards incomming @modelattribute
in spring mvc controller null (except id).
i hope can me.
here classes:
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; }
the controller:
@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"; }
the thymeleaf template
<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>
i know doesn't set other values (only found
property). or problem?
best regards
edit:
- initialized suggested
arraylist
purchaseitemlist. emptyarraylist
returned controller.
initialize purchaseitemlist in purchaselist class. public class purchaselist { private long id; private list<purchaseitem> purchaseitemlist=new arraylist<>(); private datetime purchasedate; private boolean done; }
give input field id this.
<td><input type="checkbox" th:checked="${item.found}" th:value="* {purchaseitemlist[__${status.index}__].found}" th:id='purchaselist.purchaseitemlist[__${status.index}__].found'/></td>
Comments
Post a Comment