java 分頁工具類 (非常實用的工具類 留用)
阿新 • • 發佈:2019-01-07
import java.util.List; /** * 分頁工具類 * * @author * */ public class PageBean<T> { private List<T> pageData; private Integer currentPage = Integer.valueOf(1); private Integer pageSize = Integer.valueOf(10); private Integer totalCount; public int getPageCount() { if (this.totalCount.intValue() % this.pageSize.intValue() == 0) { return this.totalCount.intValue() / this.pageSize.intValue(); } return this.totalCount.intValue() / this.pageSize.intValue() + 1; } public PageBean(List<T> pageData, Integer totalCount) { this.pageData = pageData; this.totalCount = totalCount; } public PageBean() { } public boolean isFirst() { return (this.currentPage.intValue() == 1) || (this.totalCount.intValue() == 0); } public boolean isLast() { return (this.totalCount.intValue() == 0) || (this.currentPage.intValue() >= getPageCount()); } public boolean isHasNext() { return this.currentPage.intValue() < getPageCount(); } public boolean isHasPrev() { return this.currentPage.intValue() > 1; } public Integer getNextPage() { if (this.currentPage.intValue() >= getPageCount()) { return Integer.valueOf(getPageCount()); } return Integer.valueOf(this.currentPage.intValue() + 1); } public Integer getPrevPage() { if (this.currentPage.intValue() <= 1) { return Integer.valueOf(1); } return Integer.valueOf(this.currentPage.intValue() - 1); } public List<T> getPageData() { return this.pageData; } public void setPageData(List<T> pageData) { this.pageData = pageData; } public Integer getCurrentPage() { return this.currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getPageSize() { return this.pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getTotalCount() { return this.totalCount; } public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; } }
/** * 返回新聞選項卡 * @param model * @return */ @RequestMapping("/news") public String news(Model model,HttpServletRequest req){ String currentPage = req.getParameter("currentPage"); PageBean<News> pageBean = new PageBean<News>(); pageBean.setCurrentPage(Integer.parseInt(currentPage));//預設當前頁是第一頁 pageBean.setPageSize(3);//每頁顯示條數 // List<News> news = newsmapper.selectAll(); int iStart = (pageBean.getCurrentPage()-1)*pageBean.getPageSize()+1; int iEnd = pageBean.getCurrentPage()*pageBean.getPageSize()+1; List<News> news = newsmapper.query(iStart, iEnd, null); for(News n : news){ n.setCategory(categorymapper.selectByPrimaryKey(n.getCid())); n.setStateEnum(StateEnum.getStateEnum(n.getState())); } pageBean.setPageData(news); pageBean.setTotalCount(newsmapper.getCounts(null)); model.addAttribute("model",news); model.addAttribute("pageBean",pageBean); model.addAttribute("pageCount",pageBean.getPageCount()); model.addAttribute("NextPage",pageBean.getNextPage()); model.addAttribute("PrevPage",pageBean.getPrevPage()); return "/back/news/list.jsp"; }
jsp頁面呼叫
總記錄數${pageBean.totalCount} 當前頁${pageBean.currentPage} 每頁顯示記錄數:${pageBean.pageSize } 總頁數${pageCount} 上一頁${PrevPage} 下一頁${NextPage} <nav aria-label="Page navigation" > <ul class="pagination"> <li> <a href="javascript:;" aria-label="Previous" onclick="loadPage('<%=basePath%>back/news?currentPage=${PrevPage}')"> <span aria-hidden="true">«</span> </a> </li> <c:forEach begin="1" end="${pageCount}" var="i"> <li><a href="javascript:;" onclick="loadPage('<%=basePath%>back/news?currentPage=${i}')">${i}</a></li> </c:forEach> <li> <a href="javascript:;" aria-label="Next" onclick="loadPage('<%=basePath%>back/news?currentPage=${NextPage}')"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav> <script type="text/javascript"> function loadPage(url) { $('#loadPage').html(""); $.ajax({ type : "POST", url : url, data : {}, dataType : "html", //返回值型別 使用json的話也可以,但是需要在JS中編寫迭代的html程式碼,如果格式樣式 cache : false, success : function(data) { //var json = eval('('+msg+')');//拼接的json串 $('#loadPage').empty().html(data); }, error : function(error) { alert(error); } }); } </script>