Thymeleaf 修改并发布当前对象 [英] Thymeleaf Modify and Post Current Object

查看:49
本文介绍了Thymeleaf 修改并发布当前对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单并通过 Thymeleaf 将数据发布到控制器中:

I have a form and post data into controller via Thymeleaf:

<form action="lia.html" th:action="@{/lia}" th:object="${myRequest}" method="post">

在我的 html 页面的另一个位置,如果用户单击特定按钮,我想修改该对象并将其发送到同一个控制器.

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.

我已经有了那个已经初始化的对象.按钮不是任何形式的一部分.如何使用 Thymeleaf 将该对象发送到控制器中.

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:我知道我可以通过 Javascript 发送它或将此类按钮放入表单中,但我想学习 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.

推荐答案

我认为与您正在寻找的唯一类似的方法是将 bind 与 Spring EL 表达式结合使用.

I think the only similar approach to what you're looking for is using bind with Spring EL expressions.

感谢 Spring MVC 中先进的表单域绑定功能,我们可以使用复杂的 Spring EL 表达式来绑定动态表单字段到我们的表单支持 bean.这将允许我们创建新的 Row 对象在我们的 SeedStarter bean 中,并将这些行的字段添加到我们的表单中用户请求.

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/教程/2.1/thymeleafspring.html#dynamic-fields

  • 按钮

<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";
    }
    

  • 这篇关于Thymeleaf 修改并发布当前对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆