思路图:
cstm.domain中添加类PageBean
public class PageBean<T>{
private int pc;// 当前页码page code
// private int tp;// 总页数total page
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;
}
public int getTp() {
return tr%ps==0?tr/ps:tr/ps+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;
}
}
cstm.servlet
public String find(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value=request.getParameter("pc");//获取当前页码
int pc;
if(value==null||value.trim().isEmpty()) {pc=1;}
else pc=Integer.parseInt(value);
int ps=10; //指定每页的记录数
PageBean<Customer> pb=customerService.find(pc,ps);
request.setAttribute("pb", pb);
return "f:/list.jsp";
}
cstm.dao
public PageBean<Customer> find(int pc, int ps) {
// TODO Auto-generated method stub
//1.创建PageBean
PageBean<Customer> pb=new PageBean<Customer> (); //pb(pc,tr,ts,tp,beanList)
//2.得到pc,ps
pb.setPc(pc); //设置当前页码
pb.setPs(ps); //设置每页的记录数
//3.查询数据库得到tr(总记录数),beanList(当前页的记录)
try {
String sql="select count(*) from t_customer";
Number num=(Number)qr.query(sql,new ScalarHandler());
int tr=num.intValue();
pb.setTr(tr);
sql="select * from t_customer order by cname limit ?,?";
Object [] params={ps*(pc-1),ps};
List<Customer> beanList=qr.query(sql, new BeanListHandler<Customer>(Customer.class),params);
pb.setBeanList(beanList);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //返回一个单行单列的值
return pb;
}
list.jsp中添加
>最多显示多少个页码!定为10;
> 当前页,在页码列表中的位置,定为6;
只需要当前页码来定出来页码列表!
定下来页码列表只需要两样数据:
begin、 end
需要使用pc来推算出begin和end
begin = pc – 5
end = pc + 4
计算公式:
>如果总页数<=10(列表长度),那么begin=1,end=总页数
>使用公式计算;begin=pc-5, end=pc + 4;
>头溢出:当begin<1时,让begin=1
> 尾溢出:当end>${tp}时,让end=${tp}
<center>
第${pb.pc}页/第${pb.tp}页
<a href="<c:url value='/CustomerServlet?method=find&pc=1'/>">首页</a>
<c:if test="${pb.pc > 1 }">
<a href="<c:url value='/CustomerServlet?method=find&pc=${pb.pc-1}'/>">上一页</a>
</c:if>
<!-- 计算显示的页码列表 -->
<c:choose>
<c:when test="${pb.tp}<10 }">
<c:set var='begin' value='1' />
<c:set var='end' value='${pb.tp}' />
</c:when>
<c:otherwise>
<!-- 当页数超过10时 -->
<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.pc-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='${i eq pb.pc}'>
[${i}]
</c:when>
<c:otherwise>
<a href="<c:url value='/CustomerServlet?method=find&pc=${i}' />">[${i}] </a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${pb.pc < pb.tp }">
<a href="<c:url value='/CustomerServlet?method=find&pc=${pb.pc+1}'/>">下一页</a>
</c:if>
<a href="<c:url value='/CustomerServlet?method=find&pc=${pb.tp}'/>">尾页</a>
</center>