java Spring 和 Thymeleaf:将对象从 th:each 表发送到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30926136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring and Thymeleaf: Sending an object to a controller from a th:each table
提问by AlexEnglish
I am making a table of experience data using the th:eachattribute with Thymeleafand my goal is to have a submit button in each row that, when clicked, will send an experience object to my controller that corresponds with the row I clicked the submit button in.
我正在使用Thymeleaf的th:each属性制作一个体验数据表,我的目标是在每一行中都有一个提交按钮,单击该按钮时,会将一个体验对象发送到我的控制器,该对象与我单击提交的行相对应按钮。
I have no idea whats wrong and can't seem to find anything online to help with this problem.
我不知道出了什么问题,似乎无法在网上找到任何东西来帮助解决这个问题。
Here is my section of webpage code:
这是我的网页代码部分:
<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>
Here is the method I am sending it to:
这是我将其发送到的方法:
@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;
}
回答by Aeseir
You need to fill your form with inputs as the inputs get sent:
当输入被发送时,您需要用输入填写您的表单:
<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>
This will hide your object data from user but when they click on submit, it will send the object data as required (rather than having empty form sent as you have it currently).
这将向用户隐藏您的对象数据,但是当他们单击提交时,它将根据需要发送对象数据(而不是像您当前那样发送空表单)。
回答by cicidi
As @Aeseir's answer, you need to pay attention to the th:object
and th:field
The field
can be int, double, String, List and the object should have getter and setter for its field.
In this case
作为@ Aeseir的答案,你需要注意的th:object
和th:field
的field
可以是int,双,字符串,列表和对象应为其现场getter和setter。在这种情况下
public class Experience{
String field;
List<String> list;
//getter() and setter()
}
回答by Daniel Jeney
I had the same problem. What @Aeseir said it has helped me, but instead of :
我有同样的问题。@Aeseir 所说的对我有帮助,但不是:
<input type="hidden" th:field="*{experienceDate}"/>
I needed to use:
我需要使用:
<input type="hidden" th:value="${experience.experienceDate}" name="someName"/>