欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java分页显示实现 博客分类: java  

程序员文章站 2024-03-20 08:36:46
...

JSP中:

<%
Map<String,Object> map = (Map<String,Object>)request.getAttribute("mapParms");
PageBean pb = (PageBean)request.getAttribute("pblog");
List<Map<String,Object>> logs=pb.getList();
%>
function retunPage(num) {
		var pagenow = document.getElementById("pagenow");
		var myform = document.getElementById("QueryForm");
		if (num == 1) {
			pagenow.value = 1;
		} else if (num == 2) {
			pagenow.value = Number(pagenow.value) - 1;
		} else if (num == 3) {
			pagenow.value = Number(pagenow.value) + 1;
		} else if (num == 4) {
			pagenow.value =<%=pb.getPageall()%>;
		}
		myform.submit();
	}
	function skippage() {
		var select = document.getElementById("select");
		var pagenow = document.getElementById("pagenow");
		pagenow.value = select.value;
		document.getElementById("QueryForm").submit();
	}

 

在form表单之间:

<input type="hidden" name="pagenow" id="pagenow"value="<%=pb.getPagenow()%>" />
<tr>
							
<td  colspan="5" align="left" style="font-size:12px;font-family:'微软雅黑'">总<%=pb.getRows()%>条     当前<%=pb.getPagenow()%>/<%=pb.getPageall()%><td>
	<td align="right">
		<%if (pb.getPagenow() <= 1) {%> 
		<input type="button" value="首页" disabled onclick="retunPage(1)" /> 
		<input type="button" value="上一页"disabled onclick="retunPage(2)" /> <%
 		} else {%> 
		<input type="button" value="首页" onclick="retunPage(1)" /> 
 		<input type="button" value="上一页" onclick="retunPage(2)" /> <%
 		}
 		if (pb.getPagenow() >= pb.getPageall()) {%> 
		<input type="button" value="下一页" disabled onclick="retunPage(3)" />
		<input type="button" value="尾页" disabled onclick="retunPage(4)" /> <%
		} else {%> 
		<input type="button" value="下一页" onclick="retunPage(3)" />
		<input type="button" value="尾页" onclick="retunPage(4)" /> <%}
		 %> 跳转到第<select onchange="skippage()" id="select">
		<%
		for (int i = 1; i <= pb.getPageall(); i++) {
		if (i == pb.getPagenow()) {
		%>
		<option value="<%=i%>" selected><%=i%></option>
		<%} else {
		%>
		<option value="<%=i%>"><%=i%></option>
		<%
		}
		}
		%>
		</select>
								
		</td>
</tr>

 

后台代码:

String pagenow1=request.getParameter("pagenow");
		int pagenow=0;
		if(pagenow1==null){
			pagenow=1;
		}else{
			pagenow=Integer.parseInt(pagenow1);
		}
		StringBuffer sb = new StringBuffer();
  		sb.append("SELECT top 20 * from (select *,Row_Number() OVER (Order by JDate DESC ) AS RowNumber from (select * from Log where JCompanyID='"+jCompanyID+"'");
  		System.out.println(sb);
  		StringBuffer sbCount = new StringBuffer();
  		sbCount.append("select count(*) from Log where JCompanyID='"+jCompanyID+"'");
  		PageBean pb = null;
  		try {
			pb=pageService.getPage(sbCount.toString(),sb, pagenow,20);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
  		request.setAttribute("pblog",pb);
		public PageBean getPage(String sqlCount, StringBuffer sb, int pagenow,
				int pagesize) throws NumberFormatException {
			PageBean pb = new PageBean();
			pb.setPagenow(pagenow);
			pb.setPagesize(pagesize);
			pb.setRowstart((pagenow - 1) * pagesize);
			pb.setRowend(pb.getRowstart() + pagesize);
			System.out.println(sqlCount);
			Connection conn = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				conn = SQLHelper.getConnection();
				ps = conn.prepareStatement(sqlCount);
				rs = ps.executeQuery();
				rs.next();
				pb.setRows(Integer.parseInt(rs.getString(1)));
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				SQLHelper.closeConnection(conn, ps, rs);
			}

			pb.setPageall(pb.getRows() % pagesize == 0 ? pb.getRows() / pagesize
					: pb.getRows() / pagesize + 1);
			sb.append(" ) as Work) as T where  T.RowNumber >" + pb.getRowstart());
			// 添加按报警时间逆序排列
			sb.append(" order by JDate DESC ");
			System.out.println(sb.toString());
			pb.setList(queryToList(sb.toString()));
			return pb;
		}

 根据自己项目去修改sql语句就可以了。