我有一个记录列表GET
请求显示在 UI 上并带有复选框。
@GetMapping("/list")
public String list(Model model) {
model.addAttribute("records", createRecords());
return "list";
}
这是我的Record
POJO
:
class Record {
private boolean selected;
private Integer id;
private String name;
private String phone;
//....
我在 UI 中显示如下:
<th:block th:each = "record : ${records}">
<tr>
<td><input type="checkbox" th:field="*{selected}" th:checked="${record.selected}" /></td>
<td th:field="*{id}" th:text="${record.id}" />
<td th:field="${name}" th:text="${record.name}" />
<td th:field="${phone}" th:text="${record.phone}" />
</tr>
</th:block>
我很难得到List
所选记录的数量POST
from UI
。我只从那里拿回一个对象POST
.
我想要这样的东西POST
映射:
@PostMapping("/list")
public String select(@ModelAttribute ArrayList<Record> records) {
//... at least ids of selected records
//... or all the records back with selected
请帮忙。
导致您的问题的潜在原因有很多。下面列出的三项应该可以帮助您正确映射表单:
-
您应该正确构建表单,包括使用*
减少重复的符号,例如:
<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>
如图所示Spring + Thymeleaf 教程 http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#creating-a-form
-
循环时可能需要使用双下划线符号${records}
得到每个Record
正确填写您的表格。按照Thymeleaf + Spring 教程 http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#dynamic-fields:
..__${...}__
语法是一个预处理表达式,它是在实际评估整个表达式之前评估的内部表达式。
参见示例这个问题 https://stackoverflow.com/q/33734995/2495717.
-
仔细检查您是否在 Spring 中正确处理结果列表@Controller
通过接受List<Record>
注释为@ModelAttribute
or @RequestParam
. (看起来你已经在这样做了。)
参见示例这个问题 https://stackoverflow.com/q/42341032/2495717.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)