Spring Boot:如何在 thymleaf 中绑定 POST 上的对象列表 [英] Spring Boot : How to bind list of objects on POST in thymleaf

查看:23
本文介绍了Spring Boot:如何在 thymleaf 中绑定 POST 上的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 GET 请求的记录列表,它显示在带有复选框的 UI 上.

@GetMapping("/list")公共字符串列表(模型模型){model.addAttribute("记录", createRecords());返回列表";}

这是我的记录 POJO:

class Record {选择了私有布尔值;私有整数 ID;私人字符串名称;私人字符串电话;//....

我在 UI 中显示如下:

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

我很难从 UI 获取 POST 上所选记录的 List.我只是从 POST 中取回一个对象.

我想要在 POST 映射中这样的东西:

@PostMapping("/list")公共字符串选择(@ModelAttribute ArrayList记录){//... 至少选定记录的 id//... 或所有记录返回选定的

请帮忙.

解决方案

导致您的问题的潜在原因有很多.下面列出的三项应该可以帮助您正确映射表单:

  1. 您应该正确构建表单,包括使用 * 符号来减少重复,例如:

    <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 教程中所示

  2. 在循环 ${records} 时,您可能需要使用双下划线表示法,以便在表单中正确填写每个 Record.根据 Thymeleaf + Spring 教程:<块引用>

    ..__${...}__ 语法是一个预处理表达式,它是在实际计算整个表达式之前计算的内部表达式.

    参见例如这个问题.

  3. 通过接受用 @ 注释的 List,仔细检查您是否在 Spring @Controller 中正确处理了结果列表ModelAttribute@RequestParam.(您似乎已经在这样做了.)

    参见例如这个问题.

I have a list of records on GET request which is shown on UI with a checkbox.

@GetMapping("/list")
public String list(Model model) {
    model.addAttribute("records", createRecords());
    return "list";
}

This is my Record POJO:

class Record {
    private boolean selected;   
    private Integer id; 
    private String name;    
    private String phone;
    //....

I am showing in UI as below:

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

I am having hard time to get the List of selected records on POST from UI. I just gets one object back from POST.

I want something like this in POST mapping:

@PostMapping("/list")
public String select(@ModelAttribute ArrayList<Record> records) {
    //... at least ids of selected records
    //... or all the records back with selected

Please help.

解决方案

There are a number of potential causes of your problem. The three items listed below should help you get the form mapped correctly:

  1. You should build the form correctly, including using the * notation to reduce repetition, for example:

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

    As shown in the Spring + Thymeleaf tutorial

  2. You may need to use the double-underscore notation when looping over ${records} to get each Record filled correctly in your form. As per the Thymeleaf + Spring tutorial:

    ..__${...}__ syntax is a preprocessing expression, which is an inner expression that is evaluated before actually evaluating the whole expression.

    See for example this question.

  3. Double-check that you're handling the resulting list correctly in your Spring @Controller by accepting a List<Record> annotated with @ModelAttribute or @RequestParam. (It looks like you're doing this already.)

    See for example this question.

这篇关于Spring Boot:如何在 thymleaf 中绑定 POST 上的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆