自己实现了一个分页的小功能,特此记录以备忘:
action中添加上:
int currentPage = 1; int startRow=0; int pageSize = 50; if(request.getParameter("currentPage")!=null){ currentPage=Integer.parseInt(request.getParameter("currentPage")); } if(request.getParameter("pageSize")!=null){ pageSize=Integer.parseInt(request.getParameter("pageSize")); } startRow=(currentPage-1)*pageSize; //查询代码实现…… Integer totalRows=value.length; request.setAttribute("currentPage", currentPage); request.setAttribute("totalRows", totalRows); request.setAttribute("totalPages", (totalRows-1)/pageSize+1); request.setAttribute("pageSize", pageSize);
2.jsp部分实现
//处理js function goPage(page,me,total){ var curr = $("#currentPage").val();; if("first"==me){ $("#currentPage").val(1); }else if("next"==me){ $("#currentPage").val(page); }else if("previous"==me){ $("#currentPage").val(page); }else if("last"==me){ $("#currentPage").val(page); } document.forms[0].submit() } //显示 <table class='pager-table' border='0' cellpadding='0' cellspacing='0' width='100%' align='center' > <tr class='pager-tr'> <td class='pager-td'align='right'> <input type=hidden name=pageMethod value=pager> <input type=hidden id=totalPages name="totalPages" value='${totalPages }'> 第${currentPage }页 共${totalPages }页 每页${pageSize }条 共${totalRows }条 <a href="#" onclick="goPage(1,'first',${totalPages})">首页</a> <a href="#" <c:if test="${currentPage>=2}">onclick="goPage(${currentPage-1},'next',${totalPages})"</c:if>>上一页</a> <a href="#" <c:if test="${currentPage<totalPages}">onclick="goPage(${currentPage+1},'previous',${totalPages})"</c:if>>下一页</a> <a href="#" onclick="goPage(${totalPages},'last',${totalPages})">尾页</a> 转到<select class='pager-select' id="currentPage" name=currentPage size='1' onchange=javascript:document.forms[0].submit()> <c:forEach begin="1" end="${totalPages}" var="i" step="1"> <option <c:if test="${currentPage==i }">selected=selected</c:if> value="${i }">${i }</option> </c:forEach> </select> 每页 <select id="pageSize" name='pageSize' size='1' onchange="javascript:document.forms[0].submit()"> <option value="50" <c:if test="${pageSize==50 }">selected="selected"</c:if>>50</option> <option value="100" <c:if test="${pageSize==100 }">selected="selected"</c:if>>100</option> <option value="150" <c:if test="${pageSize==150 }">selected="selected"</c:if>>150</option> <option value="200" <c:if test="${pageSize==200 }">selected="selected"</c:if>>200</option> </select>条 </td> </tr> </table>