Java SpringBoot:如何在SpringLeaf中绑定POST上的对象列表
我有一个关于Java SpringBoot:如何在SpringLeaf中绑定POST上的对象列表,java,spring-mvc,spring-boot,thymeleaf,Java,Spring Mvc,Spring Boot,Thymeleaf,我有一个关于GET请求的记录列表,该列表显示在UI上,带有一个复选框。 这是我的记录POJO: 课堂记录{ 选择私有布尔值; 私有整数id; 私有字符串名称; 私人电话; //.... 我在UI中显示如下: 我很难从UI获取POST上所选记录的列表。我只从POST获取了一个对象 我希望在POSTmapping中有类似的内容: @PostMapping(“/list”) 公共字符串选择(@modeldattribute ArrayList记录){ //…至少包含选定记录的ID //…或使
GET
请求的记录列表,该列表显示在UI上,带有一个复选框。
这是我的记录
POJO
:
课堂记录{
选择私有布尔值;
私有整数id;
私有字符串名称;
私人电话;
//....
我在UI中显示如下:
我很难从UI
获取POST
上所选记录的列表
。我只从POST
获取了一个对象
我希望在POST
mapping中有类似的内容:
@PostMapping(“/list”)
公共字符串选择(@modeldattribute ArrayList记录){
//…至少包含选定记录的ID
//…或使用所选的
请提供帮助。您的问题有许多潜在的原因。下面列出的三项将帮助您正确映射表单:
*
符号来减少重复,例如:
<th:block th:each = "record : ${records}">
<tr>
<td><input type="checkbox" th:field="*{selected}"/></td>
<td><input type="text" th:field="*{id}"/></td>
<td><input type="text" th:field="*{name}"/></td>
<td><input type="text" th:field="*{phone}"/></td>
</tr>
</th:block>
如图所示${records}
上循环时,您可能需要使用双下划线符号,以便在表单中正确填写每个记录。根据:
..{…}{code>语法是一个预处理表达式,它是一个内部表达式,在实际计算整个表达式之前进行计算
例如,见
@modeldattribute
或@RequestParam
注释的列表,再次检查您是否在Spring@Controller
中正确处理生成的列表(看起来您已经这样做了。)
例如,见
这可能是@ben3000的副本。我的限制是使用同一个对象,而不是使用包含主对象列表的包装器对象。
<th:block th:each = "record : ${records}">
<tr>
<td><input type="checkbox" th:field="*{selected}"/></td>
<td><input type="text" th:field="*{id}"/></td>
<td><input type="text" th:field="*{name}"/></td>
<td><input type="text" th:field="*{phone}"/></td>
</tr>
</th:block>