Java 在SpringMVC中,如何将数据从视图传递到控制器?
我有一个JSON对象列表,它位于工作列表中。我通过在工作列表上迭代使用每个表创建了一个表,并在thymeleaf中创建了一个表 现在,我如何将单个对象的Java 在SpringMVC中,如何将数据从视图传递到控制器?,java,json,spring-boot,thymeleaf,Java,Json,Spring Boot,Thymeleaf,我有一个JSON对象列表,它位于工作列表中。我通过在工作列表上迭代使用每个表创建了一个表,并在thymeleaf中创建了一个表 现在,我如何将单个对象的work传递回控制器,我尝试的是使用th:object 我以为它会工作,但在控制器端空值即将到来 胸腺切面 <tr th:each="work , status : ${workLists}"> <td scope="row" th:text="${status.coun
work
传递回控制器,我尝试的是使用th:object
我以为它会工作,但在控制器端空值即将到来
胸腺切面
<tr th:each="work , status : ${workLists}">
<td scope="row" th:text="${status.count}"></td>
<td>
<form th:action="@{/edit/work}" th:object="${work}" method="post">
<button type="submit" class="dropdown-item">Edit</button>
</form>
</td>
</tr>
您需要为contoller 2属性提供工作列表和工作列表。它将类似于:
@GetMapping("/edit/work")
public String editWork(Model model){
model.addAttribute("workLists", workLists);
model.addAttribute("workDTO", new Work());
return "listOfwork";
}
然后在HTML页面中,通过隐藏字段给出所选作品的值:
<table>
<tr th:each="work, stat : ${workLists}">
<td>
<form action="#" th:action="@{/edit/work}" th:object="${workDTO}" method="post">
<input type="hidden" th:attr="name='id'" th:value="${work.id}" />
<input type="hidden" th:attr="name='name'" th:value="${work.name}" />
<input type="hidden" th:attr="name='description'" th:value="${work.description}" />
<p th:text="'Id : '+${work.id}"></p>
<p th:text="'Name : '+${work.name}"></p>
<p th:text="'Description : '+${work.description}"></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</td>
</tr>
</table>
官方文档在这里@Dickens A S,我不需要任何字段我只想将已经创建的对象传递回控制器“我不需要任何字段”-然后使用javascript框架而不是模板引擎。。。
<table>
<tr th:each="work, stat : ${workLists}">
<td>
<form action="#" th:action="@{/edit/work}" th:object="${workDTO}" method="post">
<input type="hidden" th:attr="name='id'" th:value="${work.id}" />
<input type="hidden" th:attr="name='name'" th:value="${work.name}" />
<input type="hidden" th:attr="name='description'" th:value="${work.description}" />
<p th:text="'Id : '+${work.id}"></p>
<p th:text="'Name : '+${work.name}"></p>
<p th:text="'Description : '+${work.description}"></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</td>
</tr>
</table>
@PostMapping("/edit/work")
public String editWork(@ModelAttribute Work workDTO, Model model){
System.out.println(workDTO.toString());
model.addAttribute("workLists", workLists);
model.addAttribute("workDTO", new Work());
return "listOfwork";
}