spring-boot - Thymeleaf修改并发布当前对象

标签 spring-boot thymeleaf

我有一个表单,并通过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/

    相关文章:

    java - Spring Boot 应用程序在运行时卡住

    java - Spring MVC - Thymeleaf 表单与数组列表绑定(bind)

    java - Thymeleaf、Javascript 内联和迭代

    java - 用于 Java 应用程序的 Zipkin

    mysql - 如何从表中检索特定列 --- JPA 或 CrudRepository?我只想从用户表中检索电子邮件列

    spring - 如何在您的配置中修复 "Consider defining a bean of type ' org.jooq.DSLContext'。”更新到 jOOQ 3.15.0 后

    java - multipart和@RequestBody在spring可以一起用吗?

    thymeleaf - 如何根据 thymeleaf 中的条件将样式应用于 div?

    html - Intellij Idea thymeleaf 支持

    java - Spring 框架: which template engine?