[英]Spring Thymeleaf - How do I find an object id from html and pass the id to the controller?
In my html I display all my assignment, exam and attendance objects. 在我的html中,我显示了所有的作业,考试和出勤对象。 I want to find the ids of those objects and pass it over to the controller because I want the user to be able to edit those objects and in order to do that I need to find their id.
我想找到那些对象的ID并将其传递给控制器,因为我希望用户能够编辑那些对象,为此,我需要找到它们的ID。 The button to edit the object will be beside the objects name.
用于编辑对象的按钮将在对象名称旁边。 That is how I want to do it anyway.
无论如何,这就是我想要的方式。
Here is my HTML: 这是我的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>
Here is my Controller: 这是我的控制器:
@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());
Here is the solution. 这是解决方案。 I was able to find the ID of the exam that the user selected to edit and it brings them to the edit HTML page.
我能够找到用户选择进行编辑的考试的ID,并将其带到编辑HTML页面。
My 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>
Controller: 控制器:
@RequestMapping(value = "/editExam.html{examId}", method =RequestMethod.GET)
public String editExam(@PathVariable String examId) {
return "editExam";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.