Reputation: 3742
I have an ArrayList of javabeans that I iterate over in a JSP view using <c:forEach
Now I want to format the output and provide subtotals based on the groupings. Groupings are set by the sql query. One way I got it to work was using a bunch of jstl <c:set tags
in the jsp view to remember the previous row's data and then a bunch of <c:if
to make decisions.
A is worth a thousand words
Using JSTL worked locally on my PC but when I deployed from Eclipse to my development server for testing on the intranet, I got "Code Too Large For Try { " error. I think the reason is because I am using too many <c:sets
.
I have an incline that the sub totaling should be done with Java code. But then how do I correlate the subtotals with the array list of beans passed to the view? If I move the logic to my servlet, should I make another bean to model the summary rows? And then inject that bean into the array that is iterated over in the view? I'm at lost. Any ideas on a better approach?
== Edit: Added JSTL <c:forEach
loop for commenting (eliminated bunch of rows to for breviety)
<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> - <a href="?method=edit&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">edit</a> - <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> - <a href="?method=edit&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">edit</a> - <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> - <a href="?method=edit&cost_to_complete_id=<c:out value="${ctc.cost_to_complete_id}" />">edit</a> - <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>
Upvotes: 2
Views: 666
Reputation: 1108642
JSPs get after all compiled into a single try
block. The limit of the code in try
block is in most JVMs 64KB. You will get this error when the compiled JSP exceed this. I'd suggest to refactor some large parts of the JSP to another JSP page and include it by <jsp:include>
. You can pass parameters by <jsp:param>
and access them by ${param.name}
whenever necessary. To get a step further, you could also refactor common logic to a custom taglib.
Upvotes: 2