繁体   English   中英

Spring Thymeleaf-如何从html中找到对象ID并将其传递给控制器​​?

[英]Spring Thymeleaf - How do I find an object id from html and pass the id to the controller?

在我的html中,我显示了所有的作业,考试和出勤对象。 我想找到那些对象的ID并将其传递给控制器​​,因为我希望用户能够编辑那些对象,为此,我需要找到它们的ID。 用于编辑对象的按钮将在对象名称旁边。 无论如何,这就是我想要的方式。

这是我的HTML:

div th:each="subject : ${subjects}">
   Subject: <h4 th:text="${subject.subjectName}" />
 /

<h4> assignments:</h4>
<div th:each="assignment : ${subject.assignment}">
<h4 th:text="${assignment.assignmentTitle}"/>
</div>
<h4> exams:</h4>
<div th:each="exam : ${subject.exam}"> 
<h4 th:text="${exam.examTitle}"/>
<h4 th:text="${exam.examId}"/>
</div>
<h4>Attendance:</h4>
<div th:each="attendance : ${subject.attendance}">
<h4 th:text="${attendance.attendanceTitle}"/>
</div>

这是我的控制器:

@GetMapping("/allSubjects")
public String shoSubjects(@ModelAttribute("subject") @Valid UserRegistrationDto userDto, BindingResult result, Model model) {
    Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
    String email = loggedInUser.getName();   


    User user = userRepository.findByEmailAddress(email);
    List<Subject> subjects = user.getSubject();

    model.addAttribute("subjects", user.getSubject());

这是解决方案。 我能够找到用户选择进行编辑的考试的ID,并将其带到编辑HTML页面。

我的HTML:

<h4> exams:</h4>
<div th:each="exam : ${subject.exam}"> 
<h4 th:text="${exam.examTitle}"/>

<a th:href="@{/editExam.html(id=${exam.examId})}">Edit Exam</a>

控制器:

@RequestMapping(value = "/editExam.html{examId}", method =RequestMethod.GET)
public String editExam(@PathVariable String examId) {

    return "editExam";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM