JSP通用高大上分頁代碼(超管用)
先給大家展示下分頁效果,如果親們還很滿意請參考以下代碼。
在超鏈接中要保留參數(shù)
當(dāng)使用多條件查詢后,然后在點(diǎn)擊第2 頁時,這個第2頁超鏈接沒有條件了,所以會丟失條件,所以我們需要在頁面上的所有鏈接都要保留條件!
我們要把條件以一個字符串的形式保存到PageBean的url中!這個任務(wù)交給Servlet!
pagebean
package cn.itcast.cstm.domain; import java.util.List; public class PageBean<T> { private int pc;// 當(dāng)前頁碼page code //private int tp;// 總頁數(shù)total page private int tr;// 總記錄數(shù)total record private int ps;// 每頁記錄數(shù)page size private List<T> beanList;// 當(dāng)前頁的記錄 private String url;//它就是url后的條件! public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public int getPc() { return pc; } public void setPc(int pc) { this.pc = pc; } /** * 計(jì)算總頁數(shù) * @return */ public int getTp() { // 通過總記錄數(shù)和每頁記錄數(shù)來計(jì)算總頁數(shù) int tp = tr / ps; return tr%ps==0 ? tp : tp+1; } // public void setTp(int tp) { // this.tp = tp; // } 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; } }
jsp頁面
第${pb.pc }頁/共${pb.tp }頁 <a href="${pb.url }&pc=1">首頁</a> <c:if test="${pb.pc > 1 }"> <a href="${pb.url }&pc=${pb.pc-1}">上一頁</a> </c:if> <%-- 計(jì)算begin、end --%> <c:choose> <%-- 如果總頁數(shù)不足10頁,那么把所有的頁數(shù)都顯示出來! --%> <c:when test="${pb.tp <= 10 }"> <c:set var="begin" value="1" /> <c:set var="end" value="${pb.tp }" /> </c:when> <c:otherwise> <%-- 當(dāng)總頁數(shù)>10時,通過公式計(jì)算出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> <%-- 循環(huán)遍歷頁碼列表 --%> <c:forEach var="i" begin="${begin }" end="${end }"> <c:choose> <c:when test="${i eq pb.pc }"> [${i }] </c:when> <c:otherwise> <a href="${pb.url }&pc=${i}">[${i }]</a> </c:otherwise> </c:choose> </c:forEach> <c:if test="${pb.pc < pb.tp }"> <a href="${pb.url }&pc=${pb.pc+1}">下一頁</a> </c:if> <a href="${pb.url }&pc=${pb.tp}">尾頁</a>
servlet
package cn.itcast.cstm.web.servlet; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.commons.CommonUtils; import cn.itcast.cstm.domain.Customer; import cn.itcast.cstm.domain.PageBean; import cn.itcast.cstm.service.CustomerService; import cn.itcast.servlet.BaseServlet; public class CustomerServlet extends BaseServlet { private CustomerService customerService = new CustomerService(); /** * 獲取pc * * @param request * @return */ private int getPc(HttpServletRequest request) { /* * 1. 得到pc 如果pc參數(shù)不存在,說明pc=1 如果pc參數(shù)存在,需要轉(zhuǎn)換成int類型即可 */ String value = request.getParameter("pc"); if (value == null || value.trim().isEmpty()) { return 1; } return Integer.parseInt(value); } //分頁servlet public String query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // System.out.println(getUrl(request)); /* * 0. 把條件封裝到Customer對象中 1. 得到pc 2. 給定ps 3. * 使用pc和ps,以及條件對象,調(diào)用service方法得到PageBean 4. 把PageBean保存到request域中 5. * 轉(zhuǎn)發(fā)到list.jsp */ // 獲取查詢條件 Customer criteria = CommonUtils.toBean(request.getParameterMap(), Customer.class); /* * 處理GET請求方式編碼問題! */ criteria = encoding(criteria); int pc = getPc(request);// 得到pc int ps = 10;// 給定ps的值,第頁10行記錄 PageBean<Customer> pb = customerService.query(criteria, pc, ps); // 得到url,保存到pb中 pb.setUrl(getUrl(request)); request.setAttribute("pb", pb); return "f:/list.jsp"; } /** * 處理四樣 * * @param criteria * @return * @throws UnsupportedEncodingException */ private Customer encoding(Customer criteria) throws UnsupportedEncodingException { String cname = criteria.getCname(); String gender = criteria.getGender(); String cellphone = criteria.getCellphone(); String email = criteria.getEmail(); if (cname != null && !cname.trim().isEmpty()) { cname = new String(cname.getBytes("ISO-8859-1"), "utf-8"); criteria.setCname(cname); } if (gender != null && !gender.trim().isEmpty()) { gender = new String(gender.getBytes("ISO-8859-1"), "utf-8"); criteria.setGender(gender); } if (cellphone != null && !cellphone.trim().isEmpty()) { cellphone = new String(cellphone.getBytes("ISO-8859-1"), "utf-8"); criteria.setCellphone(cellphone); } if (email != null && !email.trim().isEmpty()) { email = new String(email.getBytes("ISO-8859-1"), "utf-8"); criteria.setEmail(email); } return criteria; } /** * 截取url /項(xiàng)目名/Servlet路徑?參數(shù)字符串 * * @param request * @return */ private String getUrl(HttpServletRequest request) { String contextPath = request.getContextPath();// 獲取項(xiàng)目名 String servletPath = request.getServletPath();// 獲取servletPath,即/CustomerServlet String queryString = request.getQueryString();// 獲取問號之后的參數(shù)部份 // 判斷參數(shù)部份中是否包含pc這個參數(shù),如果包含,需要截取下去,不要這一部份。 if (queryString.contains("&pc=")) { int index = queryString.lastIndexOf("&pc="); queryString = queryString.substring(0, index); } return contextPath + servletPath + "?" + queryString; } }
以上內(nèi)容是是小編給大家分享的JSP通用高大上分頁代碼(超管用),希望對大家有所幫助。
相關(guān)文章
HttpClient Post 二進(jìn)制/字節(jié)流/byte[]實(shí)例代碼
這篇文章主要介紹了 HttpClient Post 二進(jìn)制/字節(jié)流/byte[]實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-06-06jsp+servlet實(shí)現(xiàn)最簡單的增刪改查代碼分享
本文主要分享了jsp+servlet實(shí)現(xiàn)的最簡單的增刪改查代碼。代碼清晰明了,具有很好的參考價值,需要的朋友一起來看下吧2016-12-12jsp+servlet+jdbc實(shí)現(xiàn)對數(shù)據(jù)庫的增刪改查
本篇文章主要介紹了jsp+servlet+jdbc實(shí)現(xiàn)對數(shù)據(jù)庫的增刪改查,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01