JSP實現(xiàn)帶查詢條件的通用分頁組件
分頁功能的代碼就是這樣的,在需要展示列表的頁面,我們基本都需要加上分頁功能,如果某天boss想要修改分頁功能的樣式,我們不能去一個一個的改吧。
下邊給大家分享一個自己封裝的通用分頁組件。
1. 效果展示
2. 使用方式
1 )在需要顯示分頁信息的位置,直接引入pagination.jsp頁面。
<!-- 分頁內(nèi)容 --> <%@include file="/web-inf/decorators/pagination.jsp" %> <!-- 分頁內(nèi)容結(jié)束 -->
2 )在java代碼中
controller層代碼:
@requirespermissions("func_vasadmin_usermoneylist") @requestmapping("/user/money/list") public string list(model model, servletrequest request) { // 獲取所有的查詢條件參數(shù)(為了使用方便,所有查詢條件的參數(shù)命名前綴都是"search_") map<string, object> searchparams = webutils.getparametersstartingwith(request, "search_"); // 獲取分頁頁碼信息和每頁條數(shù) int pagenum = pageutil.gettargetpage(request); int pagesize = pageutil.getpagesize(request); page page = new page(pagesize, pagenum); // 查詢需要展示的內(nèi)容 list<usermoney> list = vasmoneyservice.getusermoneylist(page,searchparams); model.addattribute("data", list); model.addattribute("page", page); // 將搜索條件編碼成字符串,用于排序,分頁的url model.addattribute("searchparams", pageutil.encodeparameterstringwithprefix(searchparams, "search_")); return "vasadmin/user_money_list"; }
service層代碼:
public list<usermoney> getusermoneylist(page page, map<string, object> searchparams) { // 從map中獲取查詢條件 long userid = (long) dataconvertutil.data2(searchparams.get("userid"), dataconvertutil.datatype.long); long minmoney = (long) dataconvertutil.data2( searchparams.get("minmoney"), dataconvertutil.datatype.long); long maxmoney = (long) dataconvertutil.data2( searchparams.get("maxmoney"), dataconvertutil.datatype.long); // 查詢總條數(shù) int totleelements = vasmoneydao.getusermoneylistcount( databasename.db_tata_money, userid, minmoney, maxmoney); // 設(shè)置總條數(shù) page.settotleelements(totleelements); // 查詢需要展示的數(shù)據(jù) return vasmoneydao.getusermoneylist(databasename.db_tata_money, userid, minmoney, maxmoney, page.getstartnum(), page.getendnum()); }
**每次需要分頁,只需要操作上邊這兩步,是不是很簡單啊~~。**接下來,給大家貼出封裝的pagination.jsp代碼,和其它的一些幫助類的代碼。
**3. 代碼示例 **
pagination.jsp (封裝的通用分頁代碼)
<%@ page language="java" pageencoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <!-- 分頁信息 --> <div class="row"> <div class="col-md-3 page-number"> 每次顯示 <select class="form-control input-mini" onchange="self.location.href=options[selectedindex].value"> <option value="?pagenum=1&pagesize=10&${searchparams}" <c:if test="${page.pagesize eq '10' }">selected</c:if>>10</option> <option value="?pagenum=1&pagesize=50&${searchparams}" <c:if test="${page.pagesize eq '50' }">selected</c:if>>50</option> <option value="?pagenum=1&pagesize=100&${searchparams}" <c:if test="${page.pagesize eq '100' }">selected</c:if>>100</option> </select> 條 </div> <div class="col-md-9 paging"> <ul class="pagination"> <li><a href="#" >共 ${page.totlepages} 頁,${page.totleelements} 條</a></li> <c:if test="${page.hasprevious == false}"> <li class="disabled"><a href="#" >首頁</a></li> <li class="disabled"><a href="#" >上一頁</a></li> </c:if> <c:if test="${page.hasprevious == true}"> <li><a href="?pagenum=1&pagesize=${page.pagesize}&${searchparams}" rel="external nofollow" >首頁</a></li> <li><a href="?pagenum=${page.prepage}&pagesize=${page.pagesize}&${searchparams}" rel="external nofollow" >上一頁</a></li> </c:if> <c:foreach var="i" begin="${page.begin}" end="${page.end}"> <c:choose> <c:when test="${i == page.pagenum}"> <li class="active"><a href="?pagenum=${i}&pagesize=${page.pagesize}&${searchparams}" rel="external nofollow" rel="external nofollow" >${i}</a></li> </c:when> <c:otherwise> <li><a href="?pagenum=${i}&pagesize=${page.pagesize}&${searchparams}" rel="external nofollow" rel="external nofollow" >${i}</a></li> </c:otherwise> </c:choose> </c:foreach> <c:if test="${page.hasnext == true}"> <li><a href="?pagenum=${page.nextpage}&pagesize=${page.pagesize}&${searchparams}" rel="external nofollow" >下一頁</a></li> <li><a href="?pagenum=${page.totlepages}&pagesize=${page.pagesize}&${searchparams}" rel="external nofollow" >末頁</a></li> </c:if> <c:if test="${page.hasnext == false}"> <li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁</a></li> <li class="disabled"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >末頁</a></li> </c:if> </ul> </div> </div> <!-- 分頁信息結(jié)束 -->
page.java (分頁信息類)
public class page implements serializable { private static final long serialversionuid = 1l; public page(int pagesize, int pagenum) { if(pagesize > 0 ){ this.pagesize = pagesize; } if(pagenum > 0){ this.pagenum = pagenum; } } /** * 初始化各個值 */ public void initvalue(){ // 計算總共有多少頁 totlepages = (int)math.ceil(totleelements * 1.0 / pagesize); if(totlepages == 0){ totlepages = 1; } // 頁碼矯正 if(pagenum <= 0){ pagenum = 1; } if(pagenum > totlepages){ pagenum = totlepages; } // 是否有上一頁和首頁 if(pagenum > 1){ hasprevious = true; prepage = pagenum - 1; } // 是否有下一頁和尾頁 if(totlepages > pagenum){ hasnext = true; nextpage = pagenum + 1; } // 分頁頁碼開始數(shù)值 begin = math.max(1, pagenum - paginationsize/2); // 分頁頁碼結(jié)束數(shù)值 end = math.min(begin + (paginationsize - 1), totlepages); } /** * 分頁下標最多顯示多少頁 */ private int paginationsize = 10; /** * 頁碼開始 */ private int begin = 1; /** * 頁碼結(jié)束 */ private int end = 1; /** * 每頁顯示多少條 */ private int pagesize = 15; /** * 當(dāng)前頁碼 */ private int pagenum = 1; /** * 是否有下一頁 */ private boolean hasnext = false; /** * 是否有上一頁 */ private boolean hasprevious = false; /** * 下一頁的頁碼 */ private int nextpage; /** * 上一頁的頁碼 */ private int prepage; /** * 總頁數(shù) */ private int totlepages = 1; /** * 總條數(shù) */ private int totleelements = 0; public static int maxnum = 999999; public int getpaginationsize() { return paginationsize; } public void setpaginationsize(int paginationsize) { this.paginationsize = paginationsize; } public int getbegin() { return begin; } public void setbegin(int begin) { this.begin = begin; } public int getend() { return end; } public void setend(int end) { this.end = end; } public int getpagesize() { return pagesize; } public void setpagesize(int pagesize) { this.pagesize = pagesize; } public int getpagenum() { return pagenum; } public void setpagenum(int pagenum) { this.pagenum = pagenum; } public boolean ishasnext() { return hasnext; } public void sethasnext(boolean hasnext) { this.hasnext = hasnext; } public boolean ishasprevious() { return hasprevious; } public void sethasprevious(boolean hasprevious) { this.hasprevious = hasprevious; } public int getnextpage() { return nextpage; } public void setnextpage(int nextpage) { this.nextpage = nextpage; } public int getprepage() { return prepage; } public void setprepage(int prepage) { this.prepage = prepage; } public int gettotlepages() { return totlepages; } public void settotlepages(int totlepages) { this.totlepages = totlepages; } public int gettotleelements() { return totleelements; } public void settotleelements(int totleelements) { this.totleelements = totleelements; // 知道總條數(shù)之后,初始化一下其它值 initvalue(); } public static void main(string[] args) { system.out.println(math.min(1, 2)); } /** * 從第幾條開始查詢 * @return */ public int getstartnum(){ return (pagenum -1) * pagesize + 1; } /** * 到第幾條結(jié)束 * @return */ public int getendnum(){ return pagenum * pagesize; } }
pageutil.java (分頁輔助類)
public class pageutil implements serializable { private static final long serialversionuid = 1l; /** * 獲取目標頁碼 * @return */ public static int gettargetpage(servletrequest request){ int pagenum = 0; string pagenumstr = request.getparameter("pagenum"); if(stringutils.isnotblank(pagenumstr)){ try{ pagenum = integer.valueof(pagenumstr); }catch(exception e){ pagenum = 0; } } return pagenum; } /** * 獲取每頁條數(shù) * @return */ public static int getpagesize(servletrequest request){ int pagesize = 0; string pagesizestr = request.getparameter("pagesize"); if(stringutils.isnotblank(pagesizestr)){ try{ pagesize = integer.valueof(pagesizestr); }catch(exception e){ pagesize = 0; } } return pagesize; } /** * 將map中的查詢條件,加上前綴,以&拼接 * @param map * @param prefix 每個參數(shù)的前綴 * @return */ public static string encodeparameterstringwithprefix(map<string,object> map, string prefix){ if(null == map || map.isempty()){ return ""; } stringbuffer sb = new stringbuffer(); iterator<map.entry<string,object>> entries = map.entryset().iterator(); while (entries.hasnext()) { if(stringutils.isnotempty(sb.tostring())){ sb.append("&"); } map.entry<string,object> entry = entries.next(); sb.append(prefix); sb.append(entry.getkey()); sb.append("="); sb.append(entry.getvalue()); } return sb.tostring(); } }
jsp頁面中查詢條件參數(shù)編寫示例如下:
<!-- 查詢條件 --> <div class="col-md-10 search"> <form class="form-inline" action="${ctx }/vasadmin/user/money/list" method="get"> <div class="form-group"> <input type="text" class="form-control input-sm" name="search_userid" value="${param.search_userid }" placeholder="用戶id" onkeyup="this.value=this.value.replace(/\d/g,'')" onafterpaste="this.value=this.value.replace(/\d/g,'')"> </div> <div class="form-group"> <input type="text" class="form-control input-sm" name="search_minmoney" value="${param.search_minmoney }" placeholder="最小余額" onkeyup="this.value=this.value.replace(/\d/g,'')" onafterpaste="this.value=this.value.replace(/\d/g,'')"> </div> <div class="form-group"> <input type="text" class="form-control input-sm" name="search_maxmoney" value="${param.search_maxmoney }" placeholder="最大余額" onkeyup="this.value=this.value.replace(/\d/g,'')" onafterpaste="this.value=this.value.replace(/\d/g,'')"> </div> <div class="form-group"> <button type="submit" class="btn btn-primary btn-sm"> <span class="glyphicon glyphicon-search"> 搜索 </button> </div> </form> </div> <!-- /查詢條件 -->
源碼下載地址:分頁源碼
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持碩編程。
相關(guān)文章
- jsp+servlet實現(xiàn)文件上傳與下載功能
- EJB3.0部署消息驅(qū)動Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 秒殺系統(tǒng)Web層設(shè)計的實現(xiàn)方法
- 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁面中動態(tài)生成圖片驗證碼的方法實例
- 詳解JSP 內(nèi)置對象request常見用法
- 使用IDEA編寫jsp時EL表達式不起作用的問題及解決方法
- jsp實現(xiàn)局部刷新頁面、異步加載頁面的方法
- Jsp中request的3個基礎(chǔ)實踐
- JavaServlet的文件上傳和下載實現(xiàn)方法
- JSP頁面的靜態(tài)包含和動態(tài)包含使用方法