制作网页分页首先要有一个实体类
这个实体类内封装了分页的的所有的属性,并且还有一个得到所有属性值的方法
package cn.bdqn.entity;
import java.util.List;
public class Page {
//总记录数 从数据库查询
private int totalRecords;
//页面大小,即每页显示多少条
private int pageSize=5;
//总页数 计算出来(总记录数/页面大小)向上取整
private int totalPage;
//当前页码 从页面上传进来
private int currPage;
//每页开始查询的索引 计算出来 (当前页码-1)*页码容量
private int startIndex;
//每页新闻集合
private List customerList;
public Page(int currPage,int totalRecords){
this.totalRecords=totalRecords;
this.totalPage=totalRecords/pageSize==0?totalRecords/pageSize:totalRecords/pageSize+1;
this.currPage=currPage;
this.startIndex=(currPage-1)*pageSize;
}
//然后set/get方法
在接口类定义两个方法
//分页显示所有的客户 public List<Customer> allCustomer(int startIndex,int pageSize); //分页查询总记录数 int findPageCustomer();
接着实现接口类里面写
/**
* 分页显示客户列表
*/
@Override
public List<Customer> allCustomer(int startIndex, int pageSize) {
String sql="select * from customer limit ? ,? ";
List<Customer> list =null;
try {
list=qr.query(sql, new BeanListHandler<Customer>(Customer.class), startIndex,pageSize);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 查询记录总数 查询有多少信息
*/
@Override
public int findPageCustomer() {
String sql="select count(1) from customer";
int count=0;
try {
long lr=(Long) qr.query(sql, new ScalarHandler(1));
count=Integer.parseInt(lr+"");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
在服务实现层里的服务类(service)里写一条page的方法
//通过传过来现在的页面获取Page里的所有信息 public Page findPage(int currentPage);
在实现服务类(serviceImpl)里写实现
Page findPage(int currentPage)的方法
/**
* 通过传过来的currentPage来确定Page的信息
*/
@Override
public Page findPage(int currPage) {
int totalRecords=cd.findPageCustomer(); //总记录数
Page page = new Page(currPage,totalRecords);
List<Customer> list=cd.allCustomer(page.getStartIndex(), page.getPageSize());
page.setCustomerList(list);
return page;
}
这样原实体类的Page的所有属性都会有值了
在服务器里写个请求把page的值只给传过去
如
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//String path = request.getContextPath();
String currentPageStr = request.getParameter("pagenum");
int currentPage=1;
if(currentPageStr!=null){
currentPage=Integer.parseInt(currentPageStr);
}
CustomerService cs=new CustomerServiceImpl() ;
Page page=cs.findPage(currentPage);
request.setAttribute("page", page);
request.getRequestDispatcher("../WEB-INF/jsp/index.jsp").forward(request, response);
}
pagenum是index.jsp传过来的页面值
首次访问方服务器时pagenum是空值,index.jsp给的首页赋值为1例如
总记录数:${requestScope.page.totalRecords } 当前第${page.currPage }页/共${page.totalPage }页
<a href="${path }/servlet/start_index?pagenum=1">首页</a>
<a href="${path }/servlet/start_index?pagenum=${page.currPage-1<1?1:page.currPage-1}">上一页</a>
<a href="${path }/servlet/start_index?pagenum=${page.currPage+1<page.totalPage?page.currPage+1:page.totalPage}">下一页</a>
<a href="${path }/servlet/start_index?pagenum=${page.totalPage}">尾页</a>
<input id="bottom" type="button" value="跳转至"/><input type="text" style="width:33PX" id="num" value="2"/>
整个分页就写好了啊
原文:http://www.cnblogs.com/goodtimealways/p/7101048.html