简体   繁体   中英

How to display all iterations at once with JSTL in a JSP page

I have aa JSP page that goes through a list of items and adds to different counters based on a parameter of the items.

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

This iterates through the List and assigns the value to the correct count. My problem is since its a ForEach loop it displays the data in its own separate part each time. ie its printing out this

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

every time there is an item in the list. I tried putting this bit outside the ForEach but then I get another problem where only 1 item is ever displayed. Is there a way I can keep it inside the loop but only have it displaying after all the counters are done?

This is the function which creates the new list and populates it

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

Try with Below Code :

<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:forEach items="${studentSkills}" var="skill">
    <div class="panel panel-default">
        <div class="panel-body-center">

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

        </div>
    </div>
</c: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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM