分頁之頁面頁碼列表計算java
咱們經過看百度的分頁能夠發現如下規律:sql
1.最多顯示10個頁碼;jsp
2.當前頁在頁碼中的位置定爲六;ide
因此只須要當前頁碼來定出來頁碼列表,定下來頁碼列表只須要兩個數據:begin,end;this
須要使用pc(當前頁碼)來推算出begin和end:url
begin=pc-5;spa
end=pc+4;code
計算公式:對象
若是總頁數<=10(列表長度),那麼begin=1,end=10;不然使用計算公式:begin=pc-5以及end=pc+4;可是這樣也會致使頭溢出和尾溢出。blog
頭溢出:當begin<1時,讓begin=1,end=10;
尾溢出:當end=${tp(總頁數)}時,讓end=tp;
代碼實現:
1.首先要建立一個pagebean:
package pages; import java.util.List; public class PageBean<T> { private int pc;//當前頁碼page code // private int tp;//總頁數=總記錄數/每頁記錄數 private int tr;//總記錄數 private int ps;//每頁記錄數 private List<T> beanlist;//當前頁的記錄 public int getPc() { return pc; } public void setPc(int pc) { this.pc = pc; } public int getTp() { int num=tr/ps; int tp=tr%ps; return tp==0?num:num+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 + "]"; } }
2.servlet層:
public String findall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1.獲取頁面傳遞的pc * 2.給定ps的值 * 3.使用pc和ps調用service方法,獲得pagebean對象,保存到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"; } /* * 獲取pc */ public int getpc(HttpServletRequest request){ String value=request.getParameter("pc"); if(value==null||value.trim().isEmpty()){ return 1; } return Integer.parseInt(value); }
3.service層:
public PageBean<customer> findall(int pc, int ps){ return customerdao.findall(pc,ps); }
4.dao層:
/* * 查詢全部 */ public PageBean<customer> findall(int pc, int ps){ try { PageBean<customer> pb=new PageBean<customer>(); pb.setPc(pc); pb.setPs(ps); /* * 獲得tr */ String sql="select count(*) from t_customers"; Number trnum=(Number)qr.query(sql, new ScalarHandler()); int tr=trnum.intValue(); pb.setTr(tr); /* * 獲得beanlist */ String sql1="select * from t_customers order by cname limit ?,?"; List<customer> beanlist=qr.query(sql1, new BeanListHandler<customer>(customer.class), (pc-1)*ps,ps); pb.setBeanlist(beanlist); return pb; } catch (SQLException e) { throw new RuntimeException(e); } }
4.jsp頁面的設置:
第${pb.pc}頁/供${pb.tp}頁 <center> <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> <!-- 計算begin和end --> <c:choose> <!-- 當總頁數不足10時,所有顯示出來 --> <c:when test="${pb.tp<=10}"> <c:set var="begin" value="1"/> <c:set var="end" value="${pb.tp}"/> </c:when> <!--當總頁數大於10時,按公式計算 --> <c:otherwise> <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="${i eq pb.pc}"> [${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<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>