You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Thymeleaf:如何从HTML获取对象ID并传递至控制器?

解决方案:在Thymeleaf中添加编辑按钮并传递对象ID到后端

看起来你需要给每个关联的assignment、exam、attendance对象加上编辑按钮,把它们的ID传递到后端控制器来支持编辑操作。下面结合Thymeleaf的语法给出具体实现方案:

1. 完整HTML代码示例

假设你的Subject对象包含assignments、exams、attendances集合属性,每个子对象(比如Assignment)都有id和名称类字段(比如assignmentName),可以这样修改你的代码:

<div th:each="subject : ${subjects}">
  Subject: <h4 th:text="${subject.subjectName}" />
  
  <!-- 展示Assignments并添加编辑按钮 -->
  <h4>Assignments:</h4>
  <div th:each="assignment : ${subject.assignments}">
    <span th:text="${assignment.assignmentName}" />
    <!-- 编辑按钮:通过Thymeleaf动态生成带ID的跳转链接 -->
    <button type="button" onclick="window.location.href='@{/assignments/edit/{id}(id=${assignment.id})}'">
      Edit
    </button>
  </div>

  <!-- 展示Exams并添加编辑按钮 -->
  <h4>Exams:</h4>
  <div th:each="exam : ${subject.exams}">
    <span th:text="${exam.examName}" />
    <button type="button" onclick="window.location.href='@{/exams/edit/{id}(id=${exam.id})}'">
      Edit
    </button>
  </div>

  <!-- 展示Attendances并添加编辑按钮 -->
  <h4>Attendances:</h4>
  <div th:each="attendance : ${subject.attendances}">
    <span th:text="${attendance.attendanceName}" /> <!-- 替换成你的实际名称字段 -->
    <button type="button" onclick="window.location.href='@{/attendances/edit/{id}(id=${attendance.id})}'">
      Edit
    </button>
  </div>
</div>

2. 后端控制器对应方法示例(Spring Boot为例)

后端需要编写接收ID的控制器方法,用来加载对应对象并跳转到编辑页面:

// Assignment编辑页面跳转控制器
@GetMapping("/assignments/edit/{id}")
public String showEditAssignmentForm(@PathVariable Long id, Model model) {
  // 从服务层获取对应ID的Assignment对象
  Assignment assignment = assignmentService.getAssignmentById(id);
  model.addAttribute("assignment", assignment);
  return "edit-assignment"; // 返回编辑页面的视图名
}

// Exam编辑页面跳转控制器
@GetMapping("/exams/edit/{id}")
public String showEditExamForm(@PathVariable Long id, Model model) {
  Exam exam = examService.getExamById(id);
  model.addAttribute("exam", exam);
  return "edit-exam";
}

// Attendance编辑页面跳转控制器
@GetMapping("/attendances/edit/{id}")
public String showEditAttendanceForm(@PathVariable Long id, Model model) {
  Attendance attendance = attendanceService.getAttendanceById(id);
  model.addAttribute("attendance", attendance);
  return "edit-attendance";
}

3. 可选:更安全的POST提交方式(避免URL暴露ID)

如果不想用GET请求在URL中展示ID,可以用隐藏表单+POST提交的方式:

<div th:each="assignment : ${subject.assignments}">
  <span th:text="${assignment.assignmentName}" />
  <form th:action="@{/assignments/edit}" method="post">
    <input type="hidden" th:value="${assignment.id}" name="assignmentId" />
    <button type="submit">Edit</button>
  </form>
</div>

对应的后端POST方法:

@PostMapping("/assignments/edit")
public String processEditAssignment(@RequestParam Long assignmentId, Model model) {
  Assignment assignment = assignmentService.getAssignmentById(assignmentId);
  model.addAttribute("assignment", assignment);
  return "edit-assignment";
}

关键说明

  • 用Thymeleaf的@{}表达式生成动态URL,会自动处理项目上下文路径,避免硬编码路径的问题。
  • 遍历子对象时,直接通过${object.id}获取对象ID并传递,保证每个编辑按钮对应正确的对象。
  • 跳转编辑页面用GET请求更符合REST规范,如果是提交修改操作则建议用POST/PUT请求。

内容的提问来源于stack exchange,提问作者Jill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:10:29