【问题标题】:Thymeleaf Map Form BindingThymeleaf 地图表单绑定
【发布时间】:2016-02-28 04:12:14
【问题描述】:

db.html

<div th:each="pr, stat: *{mergeMap}">
    <tr>
        <td><input type="text" name="key" th:value="${pr.key}" /></td>
        <td><input type="text" name="value" th:value="${pr.value}" /></td>
    </tr>
</div>

在提交此输入时,我总是在 Spring Controller 中让 mergeMap 为空。应该怎么做才能得到mergeMap的值?

Controller.java

@RequestMapping(value = "/shot")
    public String saveMergeProducts(@ModelAttribute(value="prod") MergedProductInfoDTO prod, BindingResult bindingResult, 
            Model model, HttpServletRequest request) {
        System.out.println(prod.toString());
        return "forward:/backoffice/db";
    }

HTML

<form action="#" th:action="@{shot}" method="POST" th:object="${prod}">
            <tr>
            <td><span th:text="${index.index}"></span></td>
                <td><input type="text" name="id" th:value="*{id}" th:readonly="readonly" /></td>
                <td><input type="text" name="categoryName" th:value="*{categoryName}" th:readonly="readonly" /></td>
                <td><input type="text" name="subCategoryName" th:value="*{subCategoryName}" th:readonly="readonly" /></td>
                <td><input type="text" name="productBrand" th:value="*{productBrand}" /></td>
                <td><input type="text" name="productSubBrand" th:value="*{productSubBrand}" /></td>
                <td><input type="text" name="series" th:value="*{series}" /></td>
                <td><input type="text" name="model" th:value="*{model}" /></td>
            </tr>
        <tr>
            <td colspan="7">
                            <tr>
                                <th>KEY</th>
                                <th>VALUE</th>
                            </tr>
                            <div th:each="pr, stat: *{mergeMap}">
                                <tr>
                                    <td><input type="text" name="mergeMapKey" th:value="${pr.key}" /></td>
                                    <td><input type="text" name="mergeMapValue" th:value="${pr.value}" /></td>
                                </tr>
                            </div>
                        </table>
                    </td>
                    <td><input type="text" name="tags" th:value="*{tags}" /></td>
                    <td><input type="submit" value="Submit" /></td>
                </tr>

【问题讨论】:

    标签: java spring-mvc thymeleaf


    【解决方案1】:

    要访问表单支持 bean 的 Map 属性,请使用 __${...}__ 预处理器

    <div th:each="pr, stat: *{mergeMap}">
        <tr>
            <td><input type="text" name="value" th:value="${pr.key}" readonly="true"/></td>
            <td><input type="text" name="value" th:field="*{mergeMap[__${pr.key}__]}"/></td>
        </tr>
    </div>
    

    它的作用是在评估整个表达式之前先评估内部表达式。请注意,在这种情况下,不应修改 ${pr.key},以便更新将反映到绑定到表单的 bean 的 map 属性。

    参考:http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields

    【讨论】:

    • mergeMap 值未填充到 thymeleaf ui 中。
    • 地图应预先填充键。这些键将用于相应地更新地图中的值。
    • map 同时包含 key 和 value,我该如何填充它的值呢?
    猜你喜欢
    • 2019-10-21
    • 2015-02-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多