JSP根据条件在for循环中插入页脚 [英] JSP Insert footer based on condition in for loop

查看:181
本文介绍了JSP根据条件在for循环中插入页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想循环打印记录表,基于以下条件:

如果记录数大于35,我将需要暂停循环,为下一页插入一个页脚和一个新的标题,并继续它的计数直到最后一个记录。

这里的条件是只使用jsp classic scriplet。



这是我所拥有的,并且我被困住了:(以伪代码格式)

 <%int j = 0; 
for(int i = 0; i< list.size(); i ++){
col1 = list.get(i).getItem1();
col2 = list.get(i).getItem2();
col3 = list.get(i).getItem3();
j ++;

if(j == 35){%> //停止渲染页脚和下一页的页眉
< / table>
< table>
<! - 页脚内容 - >
< / table>
< table>
<! - 下一页的页眉 - >
< / table>
<%}%> <%>< t>< td><%= col1%>< / td>< td><%= col1%>< / td> COL1%GT;< / TD>< / TR>

<%}%>

这个模型的问题在于,如果我在这里使用break,我会停止循环,我不能从记录#36循环到记录结束。
我该如何去做这件事?

解决方案

如果你不想使用正确的分页,那么使用JSTL如下。

  //计数器变量初始化
< c:set var =countervalue =0scope =page/>
< c:forEach items =$ {itemList}var =item>

//计数器增量
< tr>
< td> $ {item.propertyOne}< / td>
< td> $ {item.propertyOne}< / td>
< / tr>
< c:if test =$ {counter%35 == 0}>
//在这里包含你的页脚。
< / c:if>
< / c:forEach>


I want to loop a table of records for printing, based on the following condition:

If the number of records is more than 35, I will need to pause the loop, insert a footer, and a new header for the next page and continue its count till the last record.

The condition here is to use only jsp classic scriplet.

Here is what I have and I am stuck: (in pseudo code format)

<% int j=0;
   for(int i=0; i < list.size(); i++){
    col1 = list.get(i).getItem1();
    col2 = list.get(i).getItem2();
    col3 = list.get(i).getItem3();
    j++;

    if (j==35) {%> // stops to render footer and next page's header 
    </table>
    <table>
       <!-- footer contents -->
    </table>
    <table>
       <!-- header for next page -->
    </table>
    <%}%>
<tr><td><%=col1%></td><td><%=col1%></td><td><%=col1%></td></tr>

<%}%>

the problem with this model is that if I use a break inside this if, I'd stop the loop and I can't loop from record #36 to end of record. How do I go about doing this?

解决方案

If you don't want to use proper pagination then use JSTL as below. Easier to read than scrip-lets as well, apart from the obvious benefits.

//The counter variable initialization
<c:set var="counter" value="0" scope="page"/>
<c:forEach items="${itemList}" var="item">

  //Counter increment
  <c:set var="counter" value="${counter + 1}" scope="page"/>
  <tr>
    <td>${item.propertyOne}</td>
    <td>${item.propertyOne}</td>
  </tr>
  <c:if test="${counter % 35 == 0}">
    //Include your footer here.
  </c:if>
</c:forEach>

这篇关于JSP根据条件在for循环中插入页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆