【问题标题】:JSTL Calculate total of bean values [duplicate]JSTL计算bean值的总数[重复]
【发布时间】:2012-09-12 13:45:54
【问题描述】:

在循环运行并填充数据表后,我想计算列的总数并将它们显示在最后一行。

这是我填充表格的代码...我需要构建最后一行:

<table class="data_table">
  <tr>
    <th>Source Of Issues</th>
    <th>First Issues</th>
    <th>second Issues</th>
    <th>Changes</th>
  </tr>

  <c:forEach var="bean" items="${beans}" varStatus="loopCount">
    <tr>
      <td><a href="foobar">${bean.sorCode}</a></td>
      <td class="right"><fmt:formatNumber value='${bean.firstissue}'
                             type="currency" groupingUsed='true' /></td>
      <td class="right"><fmt:formatNumber value='${bean.secondissue}'
                             type="currency" groupingUsed='true' /></td>
      <td class="right"><fmt:formatNumber value='${bean.changes}'
                             type="currency" groupingUsed='true' /></td>
    </tr>
  </c:forEach>  
</table>

【问题讨论】:

    标签: java jstl


    【解决方案1】:
    <c:set var="sum" value="${0}"/>     
    <c:forEach var="bean" items="${beans}" varStatus="loopCount">
      <c:set var="total" value="${bean.actualLabor + bean.plannedLabor}"/>
      <tr>
        <td><a href="foobar">${bean.sorCode}</a></td>
        <td class="right"><fmt:formatNumber value='${bean.firstissue}' 
                               type="currency" groupingUsed='true' /></td>
        <td class="right"><fmt:formatNumber value='${bean.secondissue}'
                               type="currency" groupingUsed='true' /></td>
        <td class="right"><fmt:formatNumber value='${changes}'
                               type="currency" groupingUsed='true' /></td>
      </tr>
      <c:set var="sum" value="${sum + changes}"/>      
    </c:forEach>
    
    <fmt:formatNumber value='${sum}' type="currency" groupingUsed='true' />      
    

    【讨论】:

    • 第一行在循环内,第二行在外。对吗?
    • 我需要栏目总数
    • 你能告诉我,你得到了什么?例外?还是计算有误?
    • 我更新了你的答案,我的答案在你的下面....你添加了 2 个不同的列。我需要一行总计显示每列的总计您还将变量名称从 sum 更改为 total
    • 你的框架是正确的
    【解决方案2】:
        <table class="data_table">
        <tr>
            <th>Source Of Issues</th>
            <th>First Issues</th>
            <th>second Issues</th>
            <th>Changes</th>
        </tr>
        <c:set var="plannedSum" value="${0}"/>
        <c:set var="actualSum" value="${0}"/> 
        <c:set var="changesSum" value="${0}"/>   
        <c:forEach var="bean" items="${beans}" varStatus="loopCount">
        <c:set var="plannedSum" value="${plannedSum + bean.firstissue}"/>
        <c:set var="actualSum" value="${actualSum + bean.secondissue}"/>  
        <c:set var="changesSum" value="${changesSum + bean.changes}"/> 
            <tr>
                <td><a href="foobar">${bean.sorCode}</a></td>
                <td class="right"><fmt:formatNumber value='${bean.firstissue}' type="currency" groupingUsed='true' /></td>
                <td class="right"><fmt:formatNumber value='${bean.secondissue}' type="currency" groupingUsed='true' /></td>
                <td class="right"><fmt:formatNumber value='${bean.changes}' type="currency" groupingUsed='true' /></td>
           </tr>
         </c:forEach>
         <tr>
                <td><a>Totals</a></td>
                <td class="right"><fmt:formatNumber value='${plannedSum}' type="currency" groupingUsed='true' /></td>
                <td class="right"><fmt:formatNumber value='${actualSum}' type="currency" groupingUsed='true' /></td>
                <td class="right"><fmt:formatNumber value='${changesSum}' type="currency" groupingUsed='true' /></td>
        </tr>
    
    
    </table>
    

    【讨论】:

      猜你喜欢
      • 2015-02-01
      • 1970-01-01
      • 2014-11-20
      • 1970-01-01
      • 2012-05-13
      • 2016-05-10
      • 2022-11-11
      • 2020-12-05
      • 2019-02-21
      相关资源
      最近更新 更多