如何使用百里香叶在具有动态列的数据表中显示输入值

我试图用数据表创建表单列表,在其中放置集合中的一些输入值。事实是,我不知道如何通过与Controller的连接来显示值,以正确显示和注册输入中的值。

这是我要显示为表单列表的DTO:

@Data
public class ComponentPeriodValues {

private int idComponent;
private String description;
private List<String> periods;
private List<BigDecimal> values;

}

所有记录的周期数均相等,我正在尝试建立如下数据表:

如何使用百里香叶在具有动态列的数据表中显示输入值

我的HTML如下:

<div id="data" th:unless="${cpv == null}" style="width:100%;height:500px;overflow:auto;">
    <form action="#" th:action="@{/planning-components/save}"
        th:object="${cpv}" method="POST">

        <table class="table table-bordered table-striped">
            <thead class="thead-dark">
                <tr>
                    <th>Description</th>
                    <th:block th:each="p : ${cpv[0].periods}">
                        <th th:text="${p}"></th>
                    </th:block>
                </tr>
            </thead>

            <tbody>
                <tr th:each="c : ${cpv}">
                    <td th:text="${c.description}" />
                    <th:block th:each="v : ${c.values}">
                        <td>
                            <input type="text" th:field="*{v}"
                                class="form-control mb-4 col-4"/>
                        </td>
                    </th:block>
                </tr>
            </tbody>        
        </table>

        <button type="submit" class="btn btn-info col-2">Save</button>
    </form>
</div>

但是,当我使用此输入字段时,出现此错误:

由以下原因导致:org.springframework.beans.NotReadablePropertyException:Bean类[java.util.ArrayList]的无效属性'v':Bean属性'v'不可读或具有无效的getter方法:返回的类型是否为getter与setter的参数类型匹配吗?

当我使用纯文本字段时,它会起作用。

所以,我的问题是:如何在此处使用输入字段来反映DTO中的更改,然后使用更新的DTO以便将更新处理到数据库中?

ddyyqqrr 回答:如何使用百里香叶在具有动态列的数据表中显示输入值

我对百里香叶语法不太了解,但是我知道spring-mvc数据绑定是如何工作的。您可能需要更改一些百里香叶语法,但这是您需要做的-

将您的ComponentPeriodValues列表包装在这样的另一个班级中

@Data
public class ComponentPeriodCommand implements Serializable {

    private static final long serialVersionUID = 1L;

    private List<ComponentPeriodValues> cpvs;
}

现在要将值绑定到列表,您的html需要在第一行第二列中看起来像这样

    <input type="text" name="cpvs[0].values[1]" class="form-control mb-4 col-4"/>

根据此baeldung post,您可以像

一样在百里香中获得索引
<div id="data" th:unless="${cpv == null}" style="width:100%;height:500px;overflow:auto;">
    <form action="#" th:action="@{/planning-components/save}"
        th:object="${command}" method="POST">

        <table class="table table-bordered table-striped">
            <thead class="thead-dark">
                <tr>
                    <th>Description</th>
                    <th:block th:each="p : ${cpv[0].periods}">
                        <th th:text="${p}"></th>
                    </th:block>
                </tr>
            </thead>

            <tbody>
                <tr th:each="c,iterC : ${command.cpvs}">
                    <td th:text="${c.description}" />
                    <th:block th:each="v,iter : ${c.values}">
                        <td>
                            <input type="text" th:field="*{cpvs[__${iterC.index}__].values[__${iter.index}__]}" class="form-control mb-4 col-4"/>
                        </td>
                    </th:block>
                </tr>
            </tbody>        
        </table>

        <button type="submit" class="btn btn-info col-2">Save</button>
    </form>
</div>
本文链接:https://www.f2er.com/3168867.html

大家都在问