在Thymeleaf中显示属于对象的所有属性时。其中一个属性是其他对象的列表,如何显示该列表?

时间:2018-03-17 12:34:33

标签: java spring spring-boot thymeleaf

我的对象和他们的关系: 用户有一个主题列表,每个主题都有一个分配列表。 我找到了当前登录的用户,并且我已经显示了除列表中的分配之外的所有主题和属于主题的每个属性。

我的控制器:

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

    User user = userRepository.findByEmailAddress(email);

    Subject subject = subjectRepository.findBySubjectName(subjectName);

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


return "allSubjects";

我的HTML:

  <div th:each="subject : ${subjects}">
Subject: <h4 th:text="${subject.subjectName}" />
Subject Grade Goal: <h4 th:text="${subject.subjectGradeGoal}" />
CA complete worth: <h4 th:text="${subject.caCompletedWorth}" />
Current Subject Results: <h4 th:text="${subject.subjectResults}" />
Remaining potential marks:<h4 th:text="${subject.maxSubRemMarks}" />
Marks until you reached your goal: <h4 th:text="${subject.marksNeededToReachGoal}" />
Can you still reach your goal?:   <h4 th:text="${subject.isGoalPossible}" />
Your highest possible grade:   <h4 th:text="${subject.highestPossibleGrade}" />

<h4 th:text="${subject.subjectName}" /> <h4> assignments:</h4>
Assignment: <h4 th:text="${subject.assignment}"/>
<!-- The above line of code does not work -->
   </div>   

1 个答案:

答案 0 :(得分:1)

您应该能够使用subject.assignment创建一个嵌套循环:

<div th:each="assignment : ${subject.assignment}">
    Assignment: <h4 th:text="${assignment.name}">[name]</h4>
</div>