使用修改后的HashMap发布对象

时间:2019-04-13 11:30:34

标签: spring thymeleaf

我正在制作一个Web应用程序,用于修改和查看数据库中存储的资源。我在将修改后的对象发布到控制器时遇到很多困难。

// This is the class of the object.
@Getter @Setter
@Document(collection = "resources")
public class Resource {
    @Id
    String id;
    String name;
    HashMap<String, String> fields = new HashMap<>();

}
// this is the controller endpoint.
@PatchMapping("resources/save")
public String save(@ModelAttribute Resource resource) {
    System.out.println(resource.getFields());
    return "redirect:/";
}
<!-- This is the HTML file where i list the fields in the resource object and input fields to modify or delete.-->

<div class="container" style="margin: 50px">

    <div class="container">
        <div>
            <div class="border-top border-bottom row m-3" style="border-bottom-width: thick;">
                <p class="col font-weight-bold mt-2">Key</p>
                <p class="col font-weight-bold mt-2">Value</p>
                <p class="col-auto"></p>
            </div>
            <form id="saveResource" th:action="@{/resources/save}" th:method="PUT">
                <div class="row m-3 border-bottom">
                    <label class="col">#</label>
                    <p class="col" th:text="${resource.getId()}"></p>
                    <p class="col-auto"></p>
                </div>
                <div class="row m-3 border-bottom">
                    <label class="col">Name</label>
                    <p class="col" th:text="${resource.getName()}"></p>
                    <p class="col-auto"></p>
                </div>
                    <div th:each="field:${resource.getFields()}" class="row m-2 border-bottom align-items-center">
                        <p class="col"th:text="${field.getKey()}" th:value="${field.getKey()}" th:field="*{field.key}" ></p>
                        <input class="form-control col mb-3" th:placeholder="${field.getValue()}" th:field="*{field.value}">
                        <!--<button class="btn-outline-danger btn col-auto">X</button> -->
                        <input class="col-auto mb-3" type="image" src="/images/remove.png">
                    </div>
            </form>
        </div>
        <button form="saveResource" class="btn btn-outline-success" type="submit">SAVE</button>
        <button class="btn btn-outline-danger" type="submit" >DISCARD</button>

    </div>


端点仅获取资源的ID,其余为空。

我想我会以错误的方式来解决这个问题,但是,我实际上该如何完成呢?

干杯。

更新: 这对我有帮助! Spring Thymeleaf pass map to form

0 个答案:

没有答案