1.为了方便在各层之间传输,将分页所需的数据封装在一个javabean中。
主要有当前页页码,每页显示记录数,总记录数,查询出的记录集合
2.如果使用的是MySQL数据库,可以使用
select * from table limit begin,end;
来方便地查询记录
3.以上的步骤都挺简单,主要在页面中显示有些麻烦,不过懂了熟了之后也不难。
1.
package cn.xc.cstm.domain;
import java.util.List;
public class PageBean<T> {
private int pc; //当前页 pageCode
// private int tp; //总页数total pages ,不应设置,要由计算得出
private int tr; //总记录数 total record
private int ps; //每页记录数 page size
private List<T> beanList; //当前页记录
public int getPc() {
return pc;
}
public void setPc(int pc) {
this.pc = pc;
}
/**
* 计算得出总页数
* @return
*/
public int getTp() {
int tp = tr / ps;
return tp % ps == 0 ? tp : tp + 1;
}
public int getTr() {
return tr;
}
public void setTr(int tr) {
this.tr = tr;
}
public int getPs() {
return ps;
}
public void setPs(int ps) {
this.ps = ps;
}
public List<T> getBeanList() {
return beanList;
}
public void setBeanList(List<T> beanList) {
this.beanList = beanList;
}
@Override
public String toString() {
return "PageBean [pc=" + pc + ", tr=" + tr + ", ps="
+ ps + ", beanList=" + beanList + "]";
}
public PageBean() {
super();
// TODO Auto-generated constructor stub
}
public PageBean(int pc, int tp, int tr, int ps, List<T> beanList) {
super();
this.pc = pc;
this.tr = tr;
this.ps = ps;
this.beanList = beanList;
}
}
2.
Servlet层
public String findAll(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException, IllegalAccessException, InvocationTargetException {
/**
* 1.得到页面传来的pc,设置ps
* 2.使用pc,ps调用service层中findAll方法,返回一个PageBean<Customer>对象pb
* 3.将pb传入request域中
* 4.转发到/list.jsp
*/
int pc = getPc(request);
int ps = 10;
PageBean<Customer> pb = customerService.findAll(pc, ps);
request.setAttribute("pb", pb);
return "f:/list.jsp";
}
service层
public PageBean<Customer> findAll(int pc, int ps){
return customerDao.findAll(pc, ps);
}
dao层
public PageBean<Customer> findAll(int pc, int ps){
/**
* 1.创建PageBean<Customer>对象,设置pc,ps
* 2.根据pc,ps 得到tr(总记录数)和 当前页记录beanList
*/
try{
PageBean<Customer> pb = new PageBean<Customer>();
pb.setPc(pc);
pb.setPs(ps);
/**
* 得到tr
*/
String sql = "select count(*) from t_customer";
Number num = (Number)qr.query(sql, new ScalarHandler());
int tr = num.intValue();
pb.setTr(tr);
/**
* 得到beanList
*/
sql = "select * from t_customer order by cname limit ?,?";
List<Customer> beanList = qr.query(sql, new BeanListHandler<Customer>(Customer.class),
(pc-1)*ps, ps);
pb.setBeanList(beanList);
return pb;
}catch(SQLException e){
throw new RuntimeException(e);
}
}
3.
<!-- 分页 -->
<center>
第${pb.pc }页/共${pb.tp }页
<a href="<c:url value='/CustomerServlet?method=findAll&pc=1'/>">首页</a>
<c:if test="${pb.pc > 1}">
<a href="<c:url value='/CustomerServlet?method=findAll&pc=${pb.pc-1 }'/>">上一页</a>
</c:if>
<!-- 计算 -->
<c:choose>
<%-- 如果总页数不足11页,就把所有的页数全显示出来 --%>
<c:when test="${pb.tp <= 10 }">
<c:set var="begin" value="1"/>
<c:set var="end" value="${pb.tp }"/>
</c:when>
<c:otherwise>
<%-- 当总页数超过10时,按公式计算begin和end --%>
<c:set var="begin" value="${pb.pc - 5 }"/>
<c:set var="end" value="${pb.pc + 4 }"/>
<%-- 头溢出 --%>
<c:if test="${begin < 1 }">
<c:set var="begin" value="1"/>
<c:set var="end" value="10"/>
</c:if>
<%-- 尾溢出 --%>
<c:if test="${end > pb.tp }">
<c:set var="begin" value="${pb.tp - 9 }"/>
<c:set var="end" value="${pb.tp }"/>
</c:if>
</c:otherwise>
</c:choose>
<!-- 设计样式 -->
<c:forEach var="i" begin="${begin }" end="${end }">
<c:choose>
<c:when test="${pb.pc eq i }">
[${i }]
</c:when>
<c:otherwise>
<a href="<c:url value='/CustomerServlet?method=findAll&pc=${i }'/>">[${i }]</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${pb.pc < pb.tp}">
<a href="<c:url value='/CustomerServlet?method=findAll&pc=${pb.pc+1 }'/>">下一页</a>
</c:if>
<a href="<c:url value='/CustomerServlet?method=findAll&pc=${pb.tp }'/>">尾页</a>
</center>