Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何使用Thymeleaf和Spring Boot将对象集合从UI绑定到后端_Html_Spring_User Interface_Spring Boot_Thymeleaf - Fatal编程技术网

Html 如何使用Thymeleaf和Spring Boot将对象集合从UI绑定到后端

Html 如何使用Thymeleaf和Spring Boot将对象集合从UI绑定到后端,html,spring,user-interface,spring-boot,thymeleaf,Html,Spring,User Interface,Spring Boot,Thymeleaf,如何将对象列表返回到后端服务?例如,UI中显示了客户列表,其中只有用户选择的客户(签入复选框)应返回到后端(控制器类)。但我无法返回所选对象 我的代码: public class CustomerType { private String customerName; private String customerMsg; private Boolean selected; // setter // getter } ​ 公共类客户{ 私有ArrayLi

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

我的代码:

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

公共类客户{
私有ArrayList customerType;
//塞特
//吸气剂
}

@GetMapping(value=“/”)
公共字符串索引(ModelMap ModelMap){
ArrayList customerType=新的ArrayList();
添加(新的customerType(“1”,“c1”,null));
添加(新的customerType(“2”,“c2”,null));
添加(新的customerType(“3”,“c3”,null));
客户=新客户();
customers.setCustomerTypes(customerType);
modelMap.put(“客户”,客户);
返回“索引”;
}
@后期映射(value=“/save”)
公共字符串保存(@ModelAttribute客户,BindingResult错误,模型){
...
...
回复“你好”;
}

=============index.html==========
...

提交

我能够在UI上显示客户列表,例如,有三个客户c1、c2、c3,如果用户选择c1和c3,那么在单击提交按钮后,这些应该映射到保存方法中的@ModelAttribute客户对象,对象应该包含两个对象c1和c3的列表,但我接收的不是2个对象,而是Null。


我无法找到出错的地方。

表单中
输入
字段的
名称
应该与
CustomerType
类中的属性名称匹配,即它们应该是
customerName
customerMsg
,以便Spring能够创建和填充相应的
客户类型
对象。

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

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

<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>


提交


这样,您将收到
客户
对象,其中包含所有通过的
客户类型
对象的列表,选中的
字段将被评估为true,用于检查记录。

您能分享您的Thymeleaf模板吗?客户类型即使在将名称更改为customerName后,我仍然得到空值,无法在后端填充这些对象请尝试更改
@modeldattribute
以直接绑定到
ArrayList customerType
@MohamedSanala甚至尝试更改仍然无法填充对象的属性。即使在设置器和获取器尝试对respectve使用name属性之后,我仍收到以下错误输入类型,例如name=“customerName”,但仍然给出相同的错误org.springframework.beans.NotReadablePropertyException:bean类[com.dto.Customers]的无效属性“customerType[0]”:bean属性“customerType[0]'不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配?您的
Customers
类是否具有方法
public ArrayList getCustomerType()
public void setCustomerType(ArrayList customerType)
?是的,它有两种方法:public ArrayList getCustomerTypes()和public void setCustomerTypes(ArrayList customerType)。签名肯定有问题,否则它会工作。仔细看你的塞特。在标有
@GetMapping
的方法中,您有一行
customers.setCustomerTypes(customerType)。setter有多个结束setCustomerTypes。若字段被称为customerType,那个么setter应该被称为setCustomerType。这可能会导致问题:谢谢我更改了setter和getter的名称,现在可以使用了,谢谢。关于“*{customerType[${iterator.index}].selected}”的详细信息,您能给出*和_uuuu的具体用途吗?
@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>
<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>