【问题标题】:How to Bind Collection of Objects from UI to Backend using Thymeleaf and Spring Boot如何使用 Thymeleaf 和 Spring Boot 将 UI 中的对象集合绑定到后端
【发布时间】:2018-04-23 23:28:52
【问题描述】:

如何将对象列表返回给后端服务?例如UI 中显示了客户列表,其中只有用户选择的客户(选中复选框)应返回到后端(控制器类)。但我无法返回所选对象。

我的代码:

public class CustomerType {
    private String customerName;
    private String customerMsg;
    private Boolean selected;
    // setter 
    // getter
}

public class Customers {
    private ArrayList<CustomerType> customerType;
    // setter 
    // getter
}

@GetMapping(value = "/")
public String index(ModelMap modelMap) {
    ArrayList<CustomerType> customerType = new ArrayList<>();
    customerType.add(new CustomerType("1", "c1", null));
    customerType.add(new CustomerType("2", "c2", null));
    customerType.add(new CustomerType("3", "c3", null));
    Customers customers = new Customers();
    customers.setCustomerTypes(customerType);
    modelMap.put("customers", customers);
    return "index";
}

@PostMapping(value = "/save")
public String save(@ModelAttribute Customers customers, BindingResult errors, Model model)  {
    ...
    ...
    return "hello";
}

========== index.html ==========
...
<form id = "form" class="col-xs-12 col-sm-4" role="form"
    th:action="@{/save}" method="post" th:object="${customers}">
    <div class="checkbox"  th:each="customerType : ${customers.customerType}" >
        <input type="checkbox" id="custType" name="custType"
            th:text="${customerType.customerName}" th:value="${customerType.customerMsg}" th:checked="${customerType.selected}"></input>
    </div>
    <div>
        <p>
            <button type="submit" class="btn btn-default">Submit</button>
        </p>
    </div>
</form>

我能够在 UI 上显示客户列表,例如,如果用户选择 c1 和 c3,则其中有三个客户 c1、c2、c3,因此在单击提交按钮后,它们应该在保存方法中映射到 @ModelAttribute 客户对象并且该对象应该包含两个对象 c1 和 c3 的列表,但我收到的不是 2 个对象。

我不知道哪里出错了。

【问题讨论】:

  • 你能分享你的 Thymeleaf 模板吗?
  • thymeleaf.org">

    客户类型

标签: html spring user-interface spring-boot thymeleaf


【解决方案1】:

在将表单发送回控制器时,请确保传输的数据反映所需的对象结构。您必须提供与CustomerType 对象的checked 字段相对应的复选框,以及与上述类的其他字段相对应的其他隐藏输入。

请将您的表单更新为如下所示:

<form id = "form" class="col-xs-12 col-sm-4" role="form" th:action="@{/save}" method="post" th:object="${customers}">
    <div class="checkbox"  th:each="customerType, iterator : ${customers.customerType}" >
        <input type="hidden" th:field=*{customerType[__${iterator.index}__].customerName} />
        <input type="hidden" th:field=*{customerType[__${iterator.index}__].customerMsg} />
        <input type="checkbox" th:field="*{customerType[__${iterator.index}__].selected}" th:text="${customerType.customerName}" ></input>
    </div>
    <div>
        <p>
            <button type="submit" class="btn btn-default">Submit</button>
        </p>
    </div>
</form>

您将收到 Customers 对象,其中包含所有已传递的 CustomerType 对象的列表,其中 selected 字段评估为 true 用于检查记录。

【讨论】:

  • 即使在设置器和获取器尝试使用 name 属性作为相应的输入类型(例如 name="customerName")后,我仍然收到以下错误,但仍然给出相同的错误 org.springframework.beans.NotReadablePropertyException: bean 类 [com.dto.Customers] 的无效属性“customerType[0]”:Bean 属性“customerType[0]”不可读或具有无效的 getter 方法:getter 的返回类型是否与二传手?
  • 您的Customers 类是否有方法public ArrayList&lt;CustomerType&gt; getCustomerType()public void setCustomerType(ArrayList&lt;CustomerType&gt; customerType)
  • 是的,它有两种方法:public ArrayList getCustomerTypes() 和 public void setCustomerTypes(ArrayList customerType)
  • 签名肯定有问题,否则会起作用。仔细看看你的二传手。在标有@GetMapping 的方法中,您有customers.setCustomerTypes(customerType); 行。 setter 有复数结尾 setCustomerTypes。如果字段被称为 customerType,那么 setter 应该被称为 setCustomerType。这可能会导致问题。
  • :谢谢我更改了 setter 和 getter 的名称,它现在可以工作了,谢谢。您能否提供有关“*{customerType[${iterator.index}].selected}”的更多信息 * 和 __ 究竟用于什么?
【解决方案2】:

表单中input 字段的name 应该与CustomerType 类中的属性名称匹配,即它们应该是customerNamecustomerMsg,以便Spring 能够创建和填充相应的CustomerType 对象。

【讨论】:

  • 即使在将名称更改为 customerName 后,我仍然得到 null ,无法在后端填充这些对象
  • 尝试将@ModelAttribute改为直接绑定ArrayList&lt;CustomerType&gt; customerType
  • @MohamedSanalla 即使尝试更改仍然无法填充对象
猜你喜欢
  • 1970-01-01
  • 2018-02-25
  • 2016-11-29
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-12
相关资源
最近更新 更多