我有一个Spring mvc页面,其中包含多行,如下所示
“详细信息”链接在循环内生成
<a href="#" class="btn btn-secondary" id="updateCategory"
th:onclick="'javascript:openModal(\'' + ${category.id} + '\');'">
<i class="fas fa-angle-double-right"></i> Details
openModal函数的代码:
function openModal(id) {
$.ajax({
url : "/admin/categories/update?catId=" + id,
success : function(data) {
$("#addCategoryModal").modal("show");
},
error : function(data) {
}
});
};
Contoller代码:
@GetMapping(value = "/update", produces = "application/json")
@ResponseBody
public ResponseEntity<Category> getUser(@RequestParam("catId") int id) {
Category category = categoryService.findOne(id);
if (category == null) {
return new ResponseEntity<Category>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Category>(category, HttpStatus.OK);
}
我希望当单击详细信息按钮时,应该使用从控制器返回的类别对象的预填充数据打开模式。
我的问题是-上面的代码执行得很好,并且由于ajax调用,我在javascript函数中得到了一个json对象,如何使用它来填充模式形式?我可以使用控制器返回的json对象以某种方式设置th:object="${category}"
的值吗?
模态形式的代码:
<form action="#" th:action="@{/admin/category/save}"
th:object="${category}" method=post>
<div class="form-group">
<input type="hidden" th:field="*{id}" /> <label for="title">Title</label>
<input type="text" th:field="*{categoryName}"
class="form-control mb-1" placeholder="Enter Category Name"
id="categoryName"></input>
<div class="error" id="categoryNameError"></div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success" id="saveCategory">Save
Changes</button>
</div>
</form>