如何在JSP页面中使用JSTL一次显示所有迭代

时间:2018-04-29 20:56:21

标签: java jsp jstl

我有一个JSP页面,它遍历一个项目列表,并根据项目的参数添加到不同的计数器。

<c:forEach items="${studentSkills}" var="skill">
        <div class="panel panel-default">
            <div class="panel-body-center">
                <c:set var="cicount" value="0" />
                <c:set var="pscount" value="0" />
                <c:set var="prcount" value="0" />
                <c:set var="jrcount" value="0" />
                <c:if test="${skill.category eq 'Community Involvement'}">
                    <p>
                        <c:set var="cicount" value="${cicount + 1}" />
                </c:if>
                <c:if test="${skill.category eq 'Personal Skill'}">
                    <p>
                        <c:set var="pscount" value="${pscount + 1}" />
                </c:if>
                <c:if test="${skill.category eq 'Physical Recreation'}">
                    <p>
                        <c:set var="prcount" value="${prcount + 1}" />
                </c:if>
                <c:if test="${skill.category eq 'Journey/Research'}">
                    <p>
                        <c:set var="jrcount" value="${jrcount + 1}" />
                </c:if>
                <p>Number of Students involved in -</p>
                <p>Community Involvement: ${cicount}</p>
                <p>Physical Recreation: ${prcount}</p>
                <p>Personal Skill: ${pscount}</p>
                <p>Journey/Research: ${jrcount}</p>
                <div id="mainwrap"></div>
            </div>
        </div>
    </c:forEach>

这将迭代List并将值分配给正确的计数。我的问题是因为它的ForEach循环每次都会在自己独立的部分显示数据。即打印出来

                <p>Number of Students involved in -</p>
                <p>Community Involvement: ${cicount}</p>
                <p>Physical Recreation: ${prcount}</p>
                <p>Personal Skill: ${pscount}</p>
                <p>Journey/Research: ${jrcount}</p>
                <div id="mainwrap"></div>

每次列表中都有一个项目。 我尝试将这个位置放在ForEach之外,但后来又遇到了另一个问题,即只显示了1个项目。 有没有办法可以将它保留在循环中,但只有在所有计数器完成后才显示它?

这是创建新列表并填充它的功能

    User currentUser = userService.findByUsername(p.getName());
    List<User> students = currentUser.getStudents();
    List<Skill> studentSkills = null;

    for (int i = 0; i < students.size(); i++) {
        String name = students.get(i).getUsername();
        User astudent = userService.findByUsername(name);
        studentSkills = astudent.getSkills();
    }

    //making sure the List is being populated
    for (Skill studentSkill : studentSkills ) {
        System.out.println(studentSkill.getSkillName());
        System.out.println(studentSkill.getCategory());
    }

    model.addAttribute("studentSkills", studentSkills);
    model.addAttribute("currentUser", currentUser);
    model.addAttribute("students", students);

0 个答案:

没有答案