我正在使用 th:each 属性和 Thymeleaf 制作一个经验数据表,我的目标是在每行中有一个提交按钮,单击该按钮时,将向我的 Controller 发送一个与我单击提交按钮的行相对应的体验对象。
我不知道出了什么问题,并且似乎无法在网上找到任何可以帮助解决此问题的内容。
这是我的网页代码部分:
<div th:unless="${#lists.isEmpty(borrower.experiences)}">
<h2>List of Experiences</h2>
<!-- <form ACTION="#" th:action="@{/initiate-edit}" th:object="${experience}"
method="POST">-->
<table id="your-table-id">
<thead>
<tr>
<td>Edit Buttons</td>
<th>Date Planted</th>
<th>Rating</th>
<th>Category</th>
<th>Dept</th>
<th>Resolution</th>
<th>Source</th>
<th>Last Update Date</th>
<th>Last Update Name</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<tr th:each="experience : ${borrower.experiences}">
<td>
<form ACTION="#" th:action="@{/initiate-edit}"
th:object="${experience}" method="POST">
<!--<a th:href="@{/initiate-edit/}">CLICK</a>-->
<button type="submit">Submit</button>
</form>
</td>
<td th:text="${experience.experienceDate}">13/01/2014</td>
<td th:text="${experience.rating}">4</td>
<td th:text="${experience.categoryShortDesc}">Account and Billing</td>
<td th:text="${experience.deptDesc}">Account and Billing</td>
<td th:text="${experience.resolutionShortTx}">Account and Billing</td>
<td th:text="${experience.source}">Account and Billing</td>
<td th:text="${experience.lastUpdateDate}">Account and Billing</td>
<td th:text="${experience.lastUpdatedName}">Account and Billing</td>
<td th:text="${experience.commentsShort}">Account and Billing</td>
</tr>
</tbody>
</table>
</div>
这是我将其发送到的方法:
@RequestMapping(value = "/initiate-edit", method = RequestMethod.POST)
public String initiateEdit(@AuthenticationPrincipal final User user,
@ModelAttribute("SpringWeb")CustomerExperience editableExperience, final Model model) {
LOG.info("THIS IS A TEST!!!" + editableExperience.getSsn());
model.addAttribute("editableExperience", editableExperience);
return EDIT_PAGE;
}
最佳答案
您需要在发送输入时填写表单:
<form ACTION="#" th:action="@{/initiate-edit}" th:object="${experience}" method="POST">
<input type="hidden" th:field="*{experienceDate}"/>
<input type="hidden" th:field="*{rating}"/>
<!-- ADD ALL THE OTHER FIELDS THAT ARE PART OF THE OBJECT -->
<button type="submit">Submit</button>
</form>
这将对用户隐藏您的对象数据,但当他们单击“提交”时,它将根据需要发送对象数据(而不是像您当前那样发送空表单)。
关于java - Spring 和 thymeleaf : Sending an object to a controller from a th:each table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30926136/