简体   繁体   English

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

[英]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.

相关问题 How do I pass an object with thymeleaf to java spring controller? - How do I pass an object with thymeleaf to java spring controller? Spring Thymeleaf-如何映射到需要从HTML传递到控制器的ID的URL? - Spring Thymeleaf - How to map to a URL that requires an ID that is passed from HTML to the controller? thymeleaf 如何将 id 从下拉列表传递到另一个 html - thymeleaf how to pass an id from dropdown list to another html Thymeleaf将数据从html id传递到thymeleaf变量 - Thymeleaf pass data from html id to thymeleaf variable 如何从HTML / thymeleaf到Spring控制器访问表单输入? - How do I access form input from HTML/thymeleaf to Spring controller? 我如何使用spring将对象从jsp传递到控制器 - How do i pass an object from jsp to controller with spring 将每个循环中的对象实例从 thymeleaf html 文件传递​​给 Spring 控制器 - Pass an instance of an object in each loop from thymeleaf html file to Spring controller 当我尝试将 HTML 中的某些内容传递给 Z38008DD81C2F4D7985ECF 中的 Controller 时,获取 Thymeleaf TemplateInputException - Getting Thymeleaf TemplateInputException when I try to pass something from the HTML to the Controller in Spring 如何将列表从视图传递到 Spring MVC 和 thymeleaf 中的 controller? - How to pass a list from the view to the controller in Spring MVC with thymeleaf? 如何将Thymeleaf模板中的隐藏值传递给控制器​​(Spring Boot)? - How can I pass a hidden value from my Thymeleaf template to a controller(Spring Boot)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM