使用Thymeleaf,如何通过arraylist显示迭代?

时间:2018-03-14 22:50:26

标签: java spring spring-boot arraylist thymeleaf

在我的控制器中,我有一个遍历arraylist的循环,并显示属于arraylist中每个对象的属性,我想知道我应该在我的百万富翁代码中放入什么,因为目前它只显示了arraylist中的第一个对象。

我的控制器类

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

    ArrayList<String> subjects = new ArrayList<String>();

    for(Subject sub:user.getSubject())
    {
        subjects.add(sub.getSubjectName());
    }
    model.addAttribute("subjects", subjects);


    if(!subjects.isEmpty()) {

    for(int i = 0; i<subjects.size(); i++) {

            String subjectName = (subjects.get(i));
            Subject subject = subjectRepository.findBySubjectName(subjectName);

            String subjectName1 = subject.getSubjectName();
            Double subjectGradeGoal = subject.getSubjectGradeGoal();
            Double caCompletedWorth = subject.getCaCompletedWorth();
            Double subjectResults = subject.getSubjectResults();
            Double maxSubRemMarks = subject.getMaxSubRemMarks();
            Double marksNeededToReachGoal = subject.getMarksNeededToReachGoal();
            Boolean isGoalPossible = subject.getIsGoalPossible();
            Double highestPossibleGrade = subject.getHighestPossibleGrade();

            model.addAttribute("subjectName",subjectName1);
            model.addAttribute("subjectGradeGoal",subjectGradeGoal);
            model.addAttribute("caCompletedWorth",caCompletedWorth);
            model.addAttribute("subjectResults",subjectResults);
            model.addAttribute("maxSubRemMarks",maxSubRemMarks);
            model.addAttribute("marksNeededToReachGoal",marksNeededToReachGoal);
            model.addAttribute("isGoalPossible",isGoalPossible);
            model.addAttribute("highestPossibleGrade",highestPossibleGrade);
            return "allSubjects";

        }


    }

return "allSubjects";

}

我的HTML类

  <>Subject: <h4 th:text="${subjectName}" /> </> 

//对于我刚刚没有为此帖添加的每个属性都有这个。

2 个答案:

答案 0 :(得分:1)

分别传递每个属性是没有意义的。您只需将主题添加到模型中,然后使用百万美元显示每个主题的属性。如果您将ArrayList<Subject>传递给百万美元,您还可以遍历每个列表,并仅显示您喜欢的属性。

然后,您可以使用百万美元创建一个列表,表格,无论您想要什么,迭代对象。例如:

<ul th:each="subject : ${subjects}">
    <li th:text="${subject.subjectName}"/>
    <li th:text="${subject.subjectName}"/>
<ul/>

答案 1 :(得分:1)

假设您的函数user.getSubject()返回一个集合或Subject对象数组,请执行以下操作

控制器方法

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

    model.addAttribute("subjects", user.getSubject());
    return "allSubjects";
}

在视图中

<div th:each="subject : ${subjects}">
    Subject: <h4 th:text="${subject.subjectName}" />
    Subject Grade Goal: <h4 th:text="${subject.subjectGradeGoal}" />
    <!-- Other Properties -->
</div>

P.S。将方法命名为user.getSubject()时返回多个主题是不好的。如果将方法名称修改为user.getSubjects(),其名称暗示将返回多个对象,那会更好。