2017-09-16 121 views
2

我有这显示在UI有一个复选框上GET请求记录列表绑定在POST对象列表。 春天开机:如何thymleaf

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

这是我RecordPOJO

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

我显示在用户界面如下:

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

我有很难得到日对POSTUI选定的记录êList。我只从POST得到一个对象。

我想在POST映射是这样的:

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

请帮助。

+0

这可能是https://stackoverflow.com/questions/36500731/how-to-bind-an-object-list-with-thymeleaf的副本。 – ben3000

+0

@ ben3000我有使用相同的对象,而不是使用具有主要对象列表包装对象的限制。 –

回答

1

有一些你的问题的潜在原因。下面列出的三个项目应该可以帮助您的形式正确映射:

  1. 你应该建立正确的形式,包括使用*符号,以减少重复,例如:

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

  2. 您可能需要遍历${records}时候能得到各Record在表单填写正确使用双下划线符号。按照the Thymeleaf + Spring tutorial

    .. __${...}__语法是一种预处理表达,这是实际评估整个表达式之前评估的内表达。

    参见例如this question

  3. 仔细检查你被接受与@ModelAttribute@RequestParam注释一List<Record>正确处理在Spring @Controller结果列表。 (看起来你已经这样做了,

    参见例如this question