【问题标题】:how we can bind a list of a list of object using thymeleaf我们如何使用 thymeleaf 绑定对象列表的列表
【发布时间】:2020-09-09 03:02:21
【问题描述】:

我有一个表单,用户可以在其中添加任意数量的 Table 对象,它还可以包含任意数量的 Columns 对象(例如构建表格在 SQL 中).. 我尝试了下面的代码,但没有任何效果,当我尝试绑定两个列表时,表单不再出现。

控制器

@ModelAttribute("page")
    public Page getTable() {
         TableColumn column = new TableColumn();
            List<TableColumn> columns = new ArrayList<>();
            columns.add(column);

            Table table = new Table();
            table.setColumns(columns);

            List<Table> tables = new ArrayList<>();
            tables.add(table);

            Page page = new Page();
            page.setTables(tables);
        return page;
    }


    @GetMapping("/scriptsqlgenerator")
    public String viewForm(@ModelAttribute("page") Page page) {
        return "ScriptSqlNext";
    }

    @PostMapping("/scriptsqlgenerator")
    public String generateScript(@ModelAttribute("page") Page page) {
    page.tables.forEach((t) ->{
    System.out.println(t.getName());
        t.getColumns().forEach((c) -> {
        System.out.println(c.getName());
        System.out.println(c.getType());
        System.out.println(c.getIndex());
        System.out.println(c.getNotnull());
        });
    });
    }

HTML

<form th:object="${page}" class="list-group" th:action="@{/filegenerated}" method="get">
<a class="list-group-item list-group-item-action" data-toggle="collapse" data-target="#target1"> Create Table </a>
    <div id="target1" class="collapse" style="margin: 30px;">
        <div id="tablelist">
            <div class="form-inline itemtable" th:each="table, itemStat :${page.tables}">
                <div class="form-group mb-2 d-none">
                   <input th:field="*{tables[__${itemStat.index}__].id}" type="text" class="form-control">
                </div>
                <div class="form-group mb-2">
                    <input th:field="*{tables[__${itemStat.index}__].name}" type="text" class="form-control" placeholder="Table name">
                </div>
                <input type="button" class="btn btn-danger mb-2 ml-2" onclick="addRow()" value="Add column">
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th scope="col" class="d-none">Id</th>
                            <th scope="col">Column Name</th>
                            <th scope="col">Type</th>
                            <th scope="col">Index</th>
                            <th scope="col">Null</th>
                        </tr>
                        </thead>
                        <tbody id="columnlist">
                        <tr class="item" th:each="column,status : 
                            ${table.columns}">
                            <td><input th:field="*{tables[__${itemStat.index}__].columns[__${status.index}__].name}" type="text" class="form-control" required></td>
                            <td><select th:field="*{tables[__${itemStat.index}__].columns[__${status.index}__].type}" id="inputState" class="form-control" required>
                                <option value="" selected 
                                 disabled>Choose</option>
                                <option th:value="${type}">int</option>
                                <option th:value="${type}">varchar</option>
                                </select>
                            </td>
                            <td><select th:field="*{tables[__${itemStat.index}__].columns[__${status.index}__].index}" id="inputState" class="form-control" required>
                                <option value="" selected 
                                 disabled>Choose</option>
                                <option th:value="${index}">on</option>
                                <option th:value="${index}">off</option>
                                </select>
                            </td>
                            <td><select th:field="*{tables[__${itemStat.index}__].columns[__${status.index}__].notnull}" id="inputState" class="form-control" required>
                                <option value="" selected 
                                 disabled>Choose</option>
                                <option th:value="${notnull}">on</option>
                                <option th:value="${notnull}">off</option>
                                </select>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    <button class="btn btn-danger mb-2 text-center" type="button" id="addTable" style="margin-top: 30px;">Add table</button>
</div>

<div class="text-center">
    <button type="submit" class="btn btn-outline-danger btn-lg" style="margin- 
     top: 50px;">Generate File</button>
</div>
</form>

对于 JS 部分,我使用一些代码来实现 addRow() 方法,该方法将向表中添加更多列,addTable() 方法将添加另一个表对象,但对我的情况没有任何作用

这是我的看法: *任何人都可以帮我解决这个问题.. 我真的需要它.. *

【问题讨论】:

    标签: javascript java spring spring-mvc thymeleaf


    【解决方案1】:

    你的方法没问题。但是您需要解决一些问题。

    在getTable 方法中,您正在为tables 和columns 设置空列表。所以在视图层中没有什么可以迭代来显示表单。改为:

    @ModelAttribute("page")
    public Page getTable() {
        Column column = new Column();
        List<Column> columns = new ArrayList<>();
        columns.add(column);
    
        Table table = new Table();
        table.setColumns(columns);
    
        List<Table> tables = new ArrayList<>();
        tables.add(table);
    
        Page page = new Page();
        page.setTables(tables);
    
        return page;
    }
    

    和

    为th:field="*{tables[__${i.index}__].name" 添加缺少的} 并关闭此input 标签。


    注意: 我不确定您想如何处理三个 select 输入。我测试了省略它们,意思是在表单中只保留Columnid和name,在这种情况下数据绑定没有任何问题。

    我也没有检查你的JS,因为你提到你还没有测试过。

    建议:
    我看到您正在从您的 POST 处理程序返回一个视图名称。看看维基百科上的以下文章。

    Post/Redirect/Get

    【讨论】:

    • 非常感谢您的回答,非常感谢。实际上,我的目标是创建一个表单,用户可以在其中输入表名称,每个表可以包含多个列,因此此后我可以生成他需要的 SQL 脚本(如 CREATE TABLE 查询)。我将编辑我的帖子会更清楚
    • @SarahAbouyassine 欢迎您。顺便说一句,您在编辑时删除了 JS 代码。关于添加新表和/或列,我建议您从后端执行此操作。向控制器发送请求,将对象添加到命令对象内的列表中并返回视图。
    • 是的,这是个好主意,但我不必刷新页面,所以我必须使用 JS 或 ajax 来实现它们
    • @SarahAbouyassine 如果你想使用 AJAX,我认为你不需要在 GET 中填充命令对象。您可以使用 JS 生成表单(无需任何命令),然后收集用户提供的数据并使用 AJAX 将它们发送到控制器。我认为这应该是一个不同于这个问题的问题,因为这个问题主要是关于数据绑定的。
    • 是的,完全正确.. 现在我正在成功绑定数据.. 我现在正在处理 JS 部分以及如何为创建的新行保留字段名称,这不是主题我的问题在这里.. 谢谢大家。
    猜你喜欢
    • 2016-02-17
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多