【问题标题】:Pagination is not working分页不起作用
【发布时间】:2010-08-04 06:34:55
【问题描述】:

我的代码中有一个问题,我卡住了表格分页不起作用。可能只检索到 5 条记录。请帮我解决我的问题:

现在才在表中显示我的 SQL 的第一条记录

<form name ="form" action="Report1.jsp" method="post" >
<%! int numPages = 0; %>
<%
String columnName = "";
int count = 0;
int totalCols = 0;
int increment = 1;
int numRows = 0;


String startIndexString = request.getParameter("startIndex");

if(startIndexString == null) {
startIndexString = "1";
}

int startIndex = Integer.parseInt(startIndexString);

try{

totalCols = 1;
%>
    <table border="1" width="84%" align="center" bgcolor=#66CCFF bordercolor=#000000 height="137">

        <tr>
        <div id="container">
            <td colspan="7" height="65">
            <p align="center"><b>Customer Information Request Form</b></td>
        </tr>
                <tr>
            <td colspan="2" height="33">
            <p align="center">
            <span style="font-size: 8pt; font-weight:700">CIR_ID</span></td>
            <td height="33">
            <span style="font-size: 8pt; font-weight:700">Bank Name</span></td>
            <td height="33" width="26%">
            <span style="font-size: 8pt; font-weight: 700">Request From</span></td>
            <td height="33" width="18%" colspan="3">
            <span style="font-weight: 700; font-size: 8pt">Created</span><span style="font-size: 8pt; font-weight: 700"> 
            Date</span></td>
        </tr>
<% 
for(int j=1; j<=totalCols && rs.next(); j++) { 

%>      
<tr bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">
<td width="3%"><span style="font-size: 8pt"><%=j=j+1%></span></td>
<td width="10%"><span style="font-size: 8pt">
<a href="cir_view.jsp?cir_id=<%=rs.getString("cir_id")%>" Title="View" onClick="return popup(this, 'Report')">
<%=rs.getString("cir_id")%>
</a>
</span> </td>
<td width="39%" ><span style="font-size: 8pt"><%=rs.getString("institution_name")%></span></td>
<td  width="26%" align="left"><span style="font-size: 8pt"><%=rs.getString("requester")%></span></td>
<td  width="10%" align="left"><span style="font-size: 8pt">
<%=rs.getString("created_date")%></span></td>
<td  width="3%" align="left"><a href="cir_delete.jsp?cir_id=<%=rs.getString("cir_id")%>" class="ask" onclick="target='_blank';">
<IMG SRC="12.png" ALT="Delete" BORDER="0" ></a></td>
<td  width="3%" align="left"><a href="cir_update.jsp?cir_id=<%=rs.getString("cir_id")%>" onClick="return popup(this, 'Report')"><IMG SRC="28.png" ALT="Edit" BORDER="0"></a></td>
</div>
</tr>


<%
}
List list = new ArrayList();

for( int i=0 ; i<100 ; i++){

list.add("item"+i);

}

numRows = list.size();

out.println(" total no. of records : "+ numRows );

int numRecordsPerPage = 5;

out.println(" Num of Records per page : " + numRecordsPerPage + "\n" );

numPages = numRows /numRecordsPerPage ;

int remain = numRows % numRecordsPerPage ;

if(remain != 0){

numPages = numPages +1 ;

}

out.println(" \n no. of pages : " + numPages );

if((startIndex + numRecordsPerPage) <= numRows) {

increment = startIndex + numRecordsPerPage ;
}
else{

if (remain == 0){

increment = startIndex + numRecordsPerPage ;

}else{

increment = startIndex + remain;
}
}

for(count = startIndex; count < increment; count++) {

%><tr><%
for(int i=1; i<=totalCols; i++) {

%><td><% out.println(list.get(count-1)); %></td><%
}
%></tr><%

}
%>

</td>
</table>
Displaying Records:
<% if(startIndex + numRecordsPerPage < numRows){%>
<%= " " + startIndex %> - <%= increment - 1 %>
<%}else{%>
<%= " " + startIndex %> - <%= numRows %>
<%}%>

<%if(startIndex != 1) {%>
<a href="Report1.jsp?startIndex=<%=startIndex-numRecordsPerPage%>">Previous</a>
<%}%>

<%increment += numRecordsPerPage ;%>
<%if(startIndex + numRecordsPerPage <= numRows){%>
<a href="Report1.jsp?startIndex=<%=startIndex+numRecordsPerPage %>">Next</a>
<%}%>
</tr>
</table>
<%
}catch(Exception exc){
out.println(exc.toString());
} // end try-catch
%>

    <p>&nbsp;</p>
</form>


</body>
<%
conn.close();
rs.close();
%>

【问题讨论】:

    标签: java jsp jdbc pagination


    【解决方案1】:

    乍一看,您只能打印一行,因为您的 totalCols 为 1,而 for cxan 中的 j 只能小于或等于 totalCols。

    totalCols = 1;
    %>
        <table border="1" width="84%" align="center" bgcolor=#66CCFF bordercolor=#000000 height="137">
    
            <tr>
            <div id="container">
                <td colspan="7" height="65">
                <p align="center"><b>Customer Information Request Form</b></td>
            </tr>
                    <tr>
                <td colspan="2" height="33">
                <p align="center">
                <span style="font-size: 8pt; font-weight:700">CIR_ID</span></td>
                <td height="33">
                <span style="font-size: 8pt; font-weight:700">Bank Name</span></td>
                <td height="33" width="26%">
                <span style="font-size: 8pt; font-weight: 700">Request From</span></td>
                <td height="33" width="18%" colspan="3">
                <span style="font-weight: 700; font-size: 8pt">Created</span><span style="font-size: 8pt; font-weight: 700"> 
                Date</span></td>
            </tr>
    <% 
    for(int j=1; j<=totalCols && rs.next(); j++) { 
    

    【讨论】:

    • 这是表格的表头。结果集应该怎么做?
    • 我已将其更改为 3,它仅显示 2 条记录。如何更正我的代码?
    • 我建议把分页的所有计算都写在for之前,然后用for(j=startIndex;j
    猜你喜欢
    • 2013-08-27
    • 1970-01-01
    • 2017-10-22
    • 2016-02-07
    • 2017-09-13
    • 2015-05-15
    • 2013-11-25
    相关资源
    最近更新 更多