文章詳情頁
JSP實現(xiàn)帶查詢條件的通用分頁組件
瀏覽:145日期:2022-06-07 14:43:17
分頁功能的代碼就是這樣的,在需要展示列表的頁面,我們基本都需要加上分頁功能,如果某天boss想要修改分頁功能的樣式,我們不能去一個一個的改吧。
下邊給大家分享一個自己封裝的通用分頁組件。
1. 效果展示
2. 使用方式
1 )在需要顯示分頁信息的位置,直接引入pagination.jsp頁面。
<!-- 分頁內容 --> <%@include file="/WEB-INF/decorators/pagination.jsp" %><!-- 分頁內容結束 -->
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); // 查詢需要展示的內容 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ù) 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> <div> 每次顯示 <select 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> <ul> <li><a href="#" >共 ${page.totlePages} 頁,${page.totleElements} 條</a></li> <c:if test="${page.hasPrevious == false}"> <li><a href="#" >首頁</a></li> <li><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><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><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >末頁</a></li> </c:if> </ul> </div></div><!-- 分頁信息結束 -->
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); // 分頁頁碼結束數(shù)值 end = Math.min(begin + (paginationSize - 1), totlePages); } /** * 分頁下標最多顯示多少頁 */ private int paginationSize = 10; /** * 頁碼開始 */ private int begin = 1; /** * 頁碼結束 */ private int end = 1; /** * 每頁顯示多少條 */ private int pageSize = 15; /** * 當前頁碼 */ 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; } /** * 到第幾條結束 * @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> <form action="${ctx }/vasadmin/user/money/list" method="get"> <div> <input type="text" 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> <input type="text" 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> <input type="text" 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> <button type="submit"> <span></span> 搜索 </button> </div> </form></div><!-- /查詢條件 -->
源碼下載地址:分頁源碼
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。
標簽:
JSP
排行榜
