【问题标题】:Html C:Foreach trying to create table with number of agesHtml C:Foreach试图创建具有年龄数的表
【发布时间】:2020-12-08 00:54:16
【问题描述】:

如果测试结果为阳性,我正在尝试创建用户年龄表。目前我让它工作,但对于每个循环,它都会创建另一行并显示结果。我正在尝试将 1 行的数字相加。我不太确定该怎么做。下面是一张桌子的图片。

下面是我创建的用于获取年龄并确保它在年龄范围之间的代码。

<!-- Infection rate per Age Group -->
 <c:set var="count2" value="0"/>
<table>
<tr>
<th>Under 20</th>
<th>20-29</th>
<th>30-39</th>
<th>40-49</th>
<th>50-59</th>
<th>60-69</th>
<th>Over 70</th>
</tr>
 <c:forEach items="${results}" var="result">
<c:set var="neg" value="Positive"/>
<!-- Set age bounds -->
<c:set var="u20" value="0"/>
<c:set var="b2029" value="0"/>
<c:set var="b3039" value="0"/>
<c:set var="b4049" value="0"/>
<c:set var="b5059" value="0"/>
<c:set var="b6069" value="0"/>
<c:set var="o70" value="0"/>
<c:set var="age" value="${result.age}"/>
<c:set var="pos" value="${result.testResult}"/>

 <c:if test="${neg eq pos}">
<!-- Check if age group is in boundaires -->
<c:if test="${age<20}">
<c:set var="u20" value="${u20 + 1 }"/>
</c:if>

<c:if test="${age >=20}">
    <c:if test="${age <=29 }">
<c:set var="b2029" value="${b2029 + 1 }"/>
</c:if>
 </c:if>
 <c:if test="${age >=30}">
    <c:if test="${age <=39 }">
 <c:set var="b3039" value="${b3039 + 1 }"/>
 </c:if>
</c:if>

<c:if test="${age >=40}">
    <c:if test="${age <49}">
<c:set var="b4049" value="${b4049 + 1 }"/>
</c:if>
</c:if>

 <c:if test="${age >=50}">
    <c:if test="${age <=59 }">
    <c:set var="b5059" value="${b5059 + 1 }"/>
  </c:if>
</c:if>

  <c:if test="${age >=60}">
    <c:if test="${age <69 }">
<c:set var="b6069" value="${b6069 + 1 }"/>
</c:if>
</c:if>
<c:if test="${age >70}">
<c:set var="o70" value="${o70 + 1 }"/>
</c:if>

</c:if>
<tr>
<td>${u20}</td>
<td>${b2029}</td>
<td>${b3039}</td>
<td>${b4049}</td>
<td>${b5059}</td>
<td>${b6069}</td>
<td>${o70}</td>
</tr>

</c:forEach>

</table>

因此,我不希望每个用户有一行(就像现在一样),我只想要 1 个单行,所有年龄相加 - 例如30-39 类别有 2 个正面,所以这应该是 2 而不是 1 和 1。谢谢您的帮助

【问题讨论】:

    标签: java html spring jsp


    【解决方案1】:

    在您当前的代码中,您在每次迭代中设置 &lt;c:set.. 变量的值,即:0,因此它始终具有值 0,并且您的 &lt;tr&gt; 标记位于 c:forEach 下,因此每次迭代都是新的row(tr) 已创建。相反,您可以更改代码,如下所示:

    <table>
    <tr>
    <th>Under 20</th>
    <!--other th-->
    </tr>
    <!-- put thse varaible outside-->
    <c:set var="neg" value="Positive"/>
    <c:set var="u20" value="0"/>
    <c:set var="b2029" value="0"/>
    <c:set var="b3039" value="0"/>
    <c:set var="b4049" value="0"/>
    <c:set var="b5059" value="0"/>
    <c:set var="b6069" value="0"/>
    <c:set var="o70" value="0"/>
     <c:forEach items="${results}" var="result">
    <c:set var="age" value="${result.age}"/>
    <c:set var="pos" value="${result.testResult}"/>
    
     <c:if test="${neg eq pos}">
    <!-- Check if age group is in boundaires -->
    <c:if test="${age<20}">
    <c:set var="u20" value="${u20 + 1 }"/>
    </c:if>
    
    <c:if test="${age >=20}">
        <c:if test="${age <=29 }">
    <c:set var="b2029" value="${b2029 + 1 }"/>
    </c:if>
     </c:if>
    <!--same if statemnts here-->
    
    </c:forEach>
    <tr>
    <td>${u20}</td>
    <td>${b2029}</td>
    <td>${b3039}</td>
    <td>${b4049}</td>
    <td>${b5059}</td>
    <td>${b6069}</td>
    <td>${o70}</td>
    </tr>
    
    </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 2023-01-29
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      相关资源
      最近更新 更多