繁体   English   中英

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

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

我有一个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>

这将遍历列表,并将值分配给正确的计数。 我的问题是,因为它有一个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之外,但又遇到另一个问题,即仅显示一项。 有没有一种方法可以将其保留在循环中,但仅在所有计数器完成后才显示?

这是创建新列表并填充它的函数

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

尝试以下代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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