Я делаю веб-приложение для изменения и просмотра ресурсов, хранящихся в базе данных.Я испытываю большие трудности с размещением измененного объекта в контроллере.
// 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>
Конечная точка получает только идентификатор ресурса, остальное равно нулю.
Я думаю, я ошибаюсь в этой проблемено как мне на самом деле это сделать?
Приветствия.
ОБНОВЛЕНИЕ: Это помогло мне! Карта прохождения Spring Thymeleaf для формирования