gpt4 book ai didi

java - Iterating over a list of Javabeans 格式化输出

转载 作者:行者123 更新时间:2023-11-30 09:50:10 25 4
gpt4 key购买 nike

我有一个 javabean 的 ArrayList,我使用 <c:forEach 在 JSP View 中对其进行迭代现在我想格式化输出并提供基于分组的小计。分组由 sql 查询设置。我让它工作的一种方法是使用一堆 jSTL <c:set tags在jsp View 中记住上一行的数据然后是一堆<c:if做出决定。

A picture值一千字

使用 JSTL 在我的 PC 上本地工作,但是当我从 Eclipse 部署到我的开发服务器以在 Intranet 上进行测试时,我收到“代码太大,无法尝试 {”错误。我认为原因是因为我使用了太多 <c:sets .

我倾向于使用 Java 代码进行小计。但是,我如何将小计与传递给 View 的 bean 数组列表关联起来呢?如果我将逻辑移到我的 servlet 中,我是否应该创建另一个 bean 来为摘要行建模?然后将该 bean 注入(inject)到在 View 中迭代的数组中?我迷路了。关于更好的方法有什么想法吗?

== 编辑:添加 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>

最佳答案

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

关于java - Iterating over a list of Javabeans 格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5318027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com