在Iavrating上输出Javabeans列表的格式输出

时间:2011-03-15 21:02:28

标签: java jsp web-applications

我有一个javabeans的ArrayList,我使用<c:forEach在JSP视图中迭代 现在我想格式化输出并根据分组提供小计。分组由sql查询设置。我开始工作的一种方法是在jsp视图中使用一堆jstl <c:set tags来记住上一行的数据,然后使用一堆<c:if来做出决定。

picture值得千言万语

使用JSTL在我的PC上本地工作但是当我从Eclipse部署到我的开发服务器以便在Intranet上进行测试时,我得到了“Code Too Large For Try {”错误。我认为原因是因为我使用了太多<c:sets

我有一个倾向,即使用Java代码完成子总计。但是,如何将小计与传递给视图的bean的数组列表相关联? 如果我将逻辑移动到我的servlet,我应该创建另一个bean来建模汇总行吗?然后将该bean注入到视图中迭代的数组中?我迷路了。有关更好方法的任何想法吗?

==编辑:添加了JSTL <c:forEach循环进行评论(为了简单而删除了一堆行)

  <c:if test="${list != null}">
  <table border="0" width="95%" cellspacing="0" cellpadding="0" class="tableBlackBorder">
      <tr>
        <td>
        <table width="100%" border="0" cellpadding="1" class="sortable" id="sortable">
              <tr>
                 <td width="115" nowrap class="phoneTableTitle">Action</td>
                 <td class="phoneTableTitle">Line Code</td>
                 <td class="phoneTableTitle">Program</td>
                 <td class="phoneTableTitle">Year</td>
                 <td class="phoneTableTitle">Jan<br>hrs</td>
                 <td class="phoneTableTitle">Feb<br>hrs</td>
                 <td class="phoneTableTitle">Nov<br>hrs</td>
                 <td class="phoneTableTitle">Dec<br>hrs</td>
                 <td class="phoneTableTitle">Total<br>hrs</td>
              </tr>

                    <c:set var="prevLinecode" value="" /> 
                    <c:set var="prevProgram" value="" /> 
                    <c:set var="totJan" value="" />
                    <c:set var="totFeb" value="" />
                    <c:set var="totNov" value="" />
                    <c:set var="totDec" value="" />
                    <c:set var="totSub" value="" />

        <c:forEach var="ctc" items="${list}" varStatus="status">

           <c:if test="${status.first}">
              <tr class="TrainingTableRowBG">
                 <td NOWRAP class="TableOutputText"><a href="?method=view&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">view</a>&nbsp;-&nbsp;<a href="?method=edit&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">edit</a>&nbsp;-&nbsp;<a href="?method=delete&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">delete</a></td>
                 <td class="TableOutputText"><c:out value="${ctc.linecode}" /></td>
                 <td class="TableOutputText" NOWRAP><c:out value="${ctc.shop_order_range.program_name}" /></td>
                 <td class="TableOutputText"><c:out value="${ctc.year}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.jan}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.feb}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.nov}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.dec}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.lineSum}" /></td>
                    <c:set var="lineJan" value="${ctc.jan}" />
                    <c:set var="lineFeb" value="${ctc.feb}" />
                    <c:set var="lineNov" value="${ctc.nov}" />
                    <c:set var="lineDec" value="${ctc.dec}" />
                    <c:set var="lineSub" value="${ctc.lineSum}" />
              </tr>  
           </c:if>

           <c:if test="${ctc.linecode == prevLinecode}" >
              <tr class="TrainingTableRowBG">
                 <td width="115" NOWRAP class="TableOutputText"><a href="?method=view&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">view</a>&nbsp;-&nbsp;<a href="?method=edit&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">edit</a>&nbsp;-&nbsp;<a href="?method=delete&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">delete</a></td>
                 <td class="TableOutputText"><c:out value="${ctc.linecode}" /></td>
                 <td class="TableOutputText" NOWRAP><c:out value="${ctc.shop_order_range.program_name}" /></td>
                 <td class="TableOutputText"><c:out value="${ctc.year}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.jan}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.feb}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.nov}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.dec}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.lineSum}" /></td>
                    <c:set var="lineJan" value="${lineJan + ctc.jan}" />
                    <c:set var="lineFeb" value="${lineFeb + ctc.feb}" />
                    <c:set var="lineNov" value="${lineNov + ctc.nov}" />
                    <c:set var="lineDec" value="${lineDec + ctc.dec}" />
                    <c:set var="lineSub" value="${lineSub + ctc.lineSum}" />
              </tr>
           </c:if>

           <c:if test="${ctc.linecode != prevLinecode && !status.first}" >

              <tr class="CTCSummary">
                 <td colspan="2">Summary For Contract: </td>
                 <td><c:out value="${prevContract}" /></td>
                 <td colspan="11" class="TableRowBGSubNav"></td>
                 <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${lineJan}" /></td>
                 <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${lineFeb}" /></td>
                 <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${lineNov}" /></td>
                 <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${lineDec}" /></td>
                 <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${lineSub}" /></td>
                    <c:set var="progJan" value="${progJan + lineJan}" />
                    <c:set var="progFeb" value="${progFeb + lineFeb}" />
                    <c:set var="progNov" value="${progNov + lineNov}" />
                    <c:set var="progDec" value="${progDec + lineDec}" />
                    <c:set var="progSub" value="${progSub + lineSub}" />
              </tr>

              <c:if test="${ctc.shop_order_range.program_name != prevProgram && !status.first}" >
                 <tr class="CTCProgramSummary">
                    <td colspan="2">Summary for Program:</td>
                    <td><c:out value="${prevProgram}" /></td>
                    <td colspan="11" class="TableRowBGSubNav"></td>
                    <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${progJan}" /></td>
                    <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${progFeb}" /></td>
                    <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${progNov}" /></td>
                    <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${progDec}" /></td>
                    <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${progSub}" /></td>
                          <c:set var="totJan" value="${totJan + progJan}" />
                          <c:set var="totFeb" value="${totFeb + progFeb}" />
                          <c:set var="totNov" value="${totNov + progNov}" />
                          <c:set var="totDec" value="${totDec + progDec}" />
                          <c:set var="totSub" value="${totSub + progSub}" />

                          <c:set var="progJan" value="" />
                          <c:set var="progFeb" value="" />
                          <c:set var="progNov" value="" />
                          <c:set var="progDec" value="" />
                          <c:set var="progSub" value="" />
                 </tr>

              </c:if>


              <tr class="TrainingTableRowBG">
                 <td width="115" NOWRAP class="TableOutputText"><a href="?method=view&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">view</a>&nbsp;-&nbsp;<a href="?method=edit&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">edit</a>&nbsp;-&nbsp;<a href="?method=delete&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">delete</a></td>
                 <td class="TableOutputText"><c:out value="${ctc.linecode}" /></td>
                 <td class="TableOutputText" NOWRAP><c:out value="${ctc.shop_order_range.program_name}" /></td>
                 <td class="TableOutputText"><c:out value="${ctc.year}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.jan}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.feb}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.nov}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.dec}" /></td>
                 <td class="TableOutputText"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${ctc.lineSum}" /></td>
                    <c:set var="lineJan" value="${ctc.jan}" />
                    <c:set var="lineFeb" value="${ctc.feb}" />
                    <c:set var="lineNov" value="${ctc.nov}" />
                    <c:set var="lineDec" value="${ctc.dec}" />
                    <c:set var="lineSub" value="${ctc.lineSum}" />
              </tr>  
           </c:if>


           <c:set var="prevLinecode" value="${ctc.linecode}" />
           <c:set var="prevProgram" value="${ctc.shop_order_range.program_name}" /> 

           <c:if test="${status.last}" >

              <tr class="CTCSummary">
                 <td colspan="2">Last Summary For Contract: </td>
                 <td><c:out value="${prevContract}" /></td>
                 <td colspan="11" class="TableRowBGSubNav"></td>
                 <td width="31" class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${lineJan}" /></td>
                 <td width="31" class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${lineFeb}" /></td>
                 <td width="31" class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${lineNov}" /></td>
                 <td width="31" class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${lineDec}" /></td>
                 <td width="31" class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${lineSub}" /></td>
                    <c:set var="progJan" value="${progJan + lineJan}" />
                    <c:set var="progFeb" value="${progFeb + lineFeb}" />
                    <c:set var="progNov" value="${progNov + lineNov}" />
                    <c:set var="progDec" value="${progDec + lineDec}" />
                    <c:set var="progSub" value="${progSub + lineSub}" />
              </tr>

              <tr class="CTCProgramSummary">
                 <td colspan="2">Summary for Program:</td>
                 <td><c:out value="${prevProgram}" /></td>
                 <td colspan="11" class="TableRowBGSubNav"></td>
                 <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${progJan}" /></td>
                 <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${progFeb}" /></td>
                 <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${progNov}" /></td>
                 <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${progDec}" /></td>
                 <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${progSub}" /></td>
                    <c:set var="totJan" value="${totJan + progJan}" />
                    <c:set var="totFeb" value="${totFeb + progFeb}" />
                    <c:set var="totNov" value="${totNov + progNov}" />
                    <c:set var="totDec" value="${totDec + progDec}" />
                    <c:set var="totSub" value="${totSub + progSub}" />

              </tr>
           </c:if>
        </c:forEach>

              <tr class="CTCTotalSummary">
                  <td colspan="2">TOTAL:</td>
                  <td></td>
                  <td colspan="11" class="TableRowBGSubNav"></td>
                  <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${totJan}" /></td>
                  <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${totFeb}" /></td>
                  <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${totNov}" /></td>
                  <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${totDec}" /></td>
                  <td class="TableRowBGSubNav"><fmt:formatNumber type="number" minIntegerDigits="1" minFractionDigits="2" value="${totSub}" /></td>
              </tr>

        </table>
        </td>
      </tr>
    </table>
  </c:if>

1 个答案:

答案 0 :(得分:2)

JSPs毕竟被编译成一个try块。 try块中的代码限制在大多数JVM中为64KB。当编译的JSP超过此值时,您将收到此错误。我建议将JSP的一些大部分重构到另一个JSP页面,并将其包含在<jsp:include>中。您可以按<jsp:param>传递参数,并在必要时通过${param.name}访问它们。为了更进一步,您还可以将常用逻辑重构为custom taglib