if (conn != null)
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return count;
}
}
//网页上分页代码
//采用电骡verycd网站上的css(blog.css.r12631和blog.css.r12631)
//请各位自行去verycd另存后查找
package Test;
public class Pager {
public static String getPager(int total,int pagesize,int pagenum)
{
int count = total/pagesize;
if (total%pagesize>0)
count++;
StringBuffer buf = new StringBuffer();
buf.append("<DIV class=\"pages-nav\" style=\"margin: 10px 7px 0px 0px;padding:0 0 20px 0px!important;padding-bottom:0;\">");
if (pagenum==1)
buf.append("<SPAN class=\"next\">« 上一页</SPAN>");
else
buf.append("<A href=\"?pagenum="+(pagenum-1)+"\" class=\"next\">上一页 »</A>");
int bound1 = ((pagenum-2)<=0)?1:(pagenum-2);
int bound2 = ((pagenum+2)>=count)?count:(pagenum+2);
for(int i=bound1;i<=bound2;i++)
{
if (i==pagenum)
buf.append("<SPAN class=\"current\">"+i+"</SPAN>");
else
buf.append("<a href='?pagenum="+i+"'>"+i+"</a>");
}
if (bound2<count)
buf.append("<SPAN>...</SPAN>");
if (pagenum==count)
buf.append("<SPAN class=\"next\">« 下一页</SPAN>");
else
buf.append("<A href=\"?pagenum="+(pagenum+1)+"\" class=\"next\">下一页 »</A>");
buf.append("</DIV>");
return buf.toString();
}
}
//JSP网页上的调用代码
//使用JSTL1.1
//采用电骡verycd网站上的css(blog.css.r12631和blog.css.r12631)
//请各位自行去verycd另存后查找
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="Test.UserInfo"%>
<%@page import="Test.UserDao"%>
<%@page import="Test.Pager"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>
<LINK type="text/css" rel="stylesheet" media="all" href="global.css.r12653">
<LINK rel="stylesheet" href="blog.css.r12631" type="text/css" media="all">
</head>
<body>
<%
int pagesize = 3;
int pagenum = 1;
if (request.getParameter("pagenum") != null)
pagenum = Integer.parseInt(request.getParameter("pagenum"));
ArrayList<UserInfo> al = UserDao.getUsers(pagesize, pagenum);
request.setAttribute("users", al);
%>
<table>
<c:forEach items="${users}" var="user">
<tr>
<td>
<c:out value="${user.id }"></c:out>
</td>
<td>
<c:out value="${user.name }"></c:out>
</td>
<td>
<c:out value="${user.age }"></c:out>
</td>
</tr>
</c:forEach>
</table>
<br>
<%= Pager.getPager(UserDao.getCount(),pagesize,pagenum) %>
</body>
</html>
责任编辑:小草