问题
I have a form and post data into controller via Thymeleaf:
<form action="lia.html" th:action="@{/lia}" th:object="${myRequest}" method="post">
At another place of my html page, if a user click a particular button, I want to modify that object and send it to same controller.
I have already that object which has been initialised. Button is not a part of any form. How can I send that object into a controller with Thymeleaf.
PS: I know that I can send it via Javascript or put such buttons into a form but I want to learn the Thymeleaf way.
回答1:
I think the only similar approach to what you're looking for is using bind with Spring EL expressions.
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.
Take a look the next link, where is good example:
http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields
The button
<button type="submit" name="removeRow" th:value="${rowStat.index}" th:text="#{seedstarter.row.remove}">Remove row</button>
Request Mapping
@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"; }
来源:https://stackoverflow.com/questions/40020127/thymeleaf-modify-and-post-current-object