Jill
Jill

Reputation: 441

In Thymeleaf when displaying all attributes belonging to an object & one of those attributes is a list of other objects, how do I display that list?

My objects and their relationships: User has a list of subjects, each subject has a list of assignments. I have found the current logged in user and I have displayed all their subjects and each attribute belonging to the subject except the assignments in the list.

My controller:

   @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";

My 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>   

Upvotes: 1

Views: 283

Answers (1)

user9486023
user9486023

Reputation:

You should be able to create a nested loop with the subject.assignment:

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

Upvotes: 1

Related Questions