我有一个表单,并通过Thymeleaf将数据发布到 Controller 中:
<form action="lia.html" th:action="@{/lia}" th:object="${myRequest}" method="post">
在我的html页面的另一个位置,如果用户单击一个特定的按钮,我想修改该对象并将其发送到同一 Controller 。
我已经有一个已初始化的对象。按钮不是任何形式的一部分。如何使用Thymeleaf将对象发送到 Controller 中。
PS:我知道我可以通过Javascript发送或将这样的按钮放入表单中,但是我想学习Thymeleaf方式。
最佳答案
我认为,与您要查找的内容唯一相似的方法是对Spring EL表达式使用bind。
Thanks to the advanced form-field binding capabilities in Spring MVC, we can use complex Spring EL expressions to bind dynamic form fields to our form-backing bean. This will allow us to create new Row objects in our SeedStarter bean, and to add those rows’ fields to our form at user request.
看下一个链接,哪里是个好例子:
http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields
<button type="submit" name="removeRow"
th:value="${rowStat.index}" th:text="#{seedstarter.row.remove}">Remove row</button>
@RequestMapping(value="/seedstartermng", params={"removeRow"})
public String removeRow(
final SeedStarter seedStarter, final BindingResult bindingResult,
final HttpServletRequest req) {
final Integer rowId = Integer.valueOf(req.getParameter("removeRow"));
seedStarter.getRows().remove(rowId.intValue());
return "seedstartermng";
}
关于spring-boot - Thymeleaf修改并发布当前对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40020127/