Spring boot 如何使用Spring Boot/Thymeleaf在引导模式中加载对象?

Spring boot 如何使用Spring Boot/Thymeleaf在引导模式中加载对象?,spring-boot,bootstrap-modal,thymeleaf,bootstrap-4,Spring Boot,Bootstrap Modal,Thymeleaf,Bootstrap 4,我有一张满是任务的桌子: <table class="table" id="tasksTable"> <thead class="thead-inverse"> <tr> <th>ID</th> <th>Title</th> <th>Actions</th> </tr>

我有一张满是任务的桌子:

<table class="table" id="tasksTable">
    <thead class="thead-inverse">
        <tr>
            <th>ID</th>
            <th>Title</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <tr data-th-each="task : ${tasks}">
            <td data-th-text="${task.id}">Task ID</td>
            <td data-th-text="${task.title}">Task Title</td>
            <td><a th:href="@{/tasks/delete/{id}(id=${task.id})}"
                class="btn btn-danger">Delete</a> -
                <button class="btn btn-warning" data-toggle="modal"
                    data-th-id="${task.id}" data-target="#updateTaskModal">Update</button></td>
        </tr>
    </tbody>
</table>

身份证件
标题
行动
任务ID
任务标题
-
更新
该列表由如下控制器发送:

@GetMapping(path = "/")
public String getAllUsersView(Model model) {
    List<User> users = new ArrayList<>();
    List<Task> tasks = new ArrayList<>();
    User user = new User();
    Task task = new Task();

    userRepository.findAll().forEach(users::add);
    taskRepository.findAll().forEach(tasks::add);

    model.addAttribute("users", users);
    model.addAttribute("tasks", tasks);
    model.addAttribute("user", user);
    model.addAttribute("task", task);

    return "view";
}
@GetMapping(path=“/”)
公共字符串getAllUsersView(模型){
列表用户=新建ArrayList();
列表任务=新建ArrayList();
用户=新用户();
任务=新任务();
userRepository.findAll().forEach(users::add);
taskRepository.findAll().forEach(tasks::add);
model.addAttribute(“用户”,用户);
model.addAttribute(“任务”,任务);
model.addAttribute(“用户”,用户);
model.addAttribute(“任务”,任务);
返回“视图”;
}
我想从表中选择一个任务,并将其发送到modal。例如,假设我有10项任务。我想选择任务5并能够更新它。当我单击“更新”时,我可以打开一个模式,但我不知道如何用特定任务的数据填充表单,它都是空白的

这是我的情态:

  <div id="updateTaskModal" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Update Task</h4>
                </div>
                <div class="modal-body">

                    <form id="updateNewTask" action="#" th:action="@{/tasks/update}"
                        th:object="${task}" method="put">

                        <input type="text" class="form-control" name="title" id="title"
                            th:field="*{title}" placeholder="Task Title" />

                        <hr />

                        <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-success pull-right">Update</button>

                    </form>
                </div>
            </div>
        </div>
    </div>

&时代;
更新任务

取消 更新

实现这一目标的最佳实践是什么?是否可以通过按钮发送整个对象?如果没有,如何将对象加载到模态中?我可以通过/tasks/{taskId}检索对象的JSON版本,但我不知道如何从模式调用它。

您可以使用Javascript将内容加载到现有模式。实现这一点的“最佳”方法是,使用ThymlLeaf将服务器上的对象呈现为一些HTML,然后通过Ajax获取该响应,并将其注入到模式框中的一些div标记中