jsp实现简单的分页

效果如下:

<%-- Document : page Created on : 2014-11-18, 8:55:02 Author : HJZ --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <script language= "javascript"> function dumpTo(tot) { //函数参数定义时不加var,否则出错 var pageMsg = document.getElementById("pageTo").value; var patrn=/^\d+$/; if (patrn.test(pageMsg)) { if(parseInt(pageMsg)<1 || parseInt(pageMsg)>parseInt(tot)) return; window.location.href="page.jsp?page=" + pageMsg; } } </script> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>分页显示</title> </head> <body> <% int pageCur = 0; int pageTot = 20; int pageBegin = 0; String curPage = request.getParameter("page"); if(curPage == null) pageCur = 1; else pageCur = Integer.valueOf(curPage); pageBegin = pageCur-5; if(pageBegin < 1) pageBegin = 1; %> <talbe border="10"> <tr> <%if(pageCur!=1){%> <td> <input type="submit" name="skip" value="上一页" onclick="javascript:location.href=‘page.jsp?page=<%=pageCur-1%>‘"> </td> <%}%> <% for(int i=pageBegin, j=1; j<=10 && i<=pageTot; ++i, ++j) {%> <%if(i == pageCur){%> <td> <%=i%> </td> <%} else {%> <td> <input type="submit" name="skip" value="<%=i%>" onclick="javascript:location.href=‘page.jsp?page=<%=i%>‘"> </td> <%}%> <% } %> <%if(pageCur!=pageTot){%> <td> <input type="submit" name="skip" value="下一页" onclick="javascript:location.href=‘page.jsp?page=<%=pageCur+1%>‘"> </td> <%}%> <td><%=pageTot%></td> <td width="105px"> 向第<input type="text" id="pageTo"></td> <td> <input type="button" name="skip" value="跳转" onclick="dumpTo(<%=pageTot%>)"> <!--传递总页数--> </td> </tr> </table> </body> </html>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。