Freemarker分页
有两个参考类,
import java.util.List; /** * 分页显示对象 * @param <T> */ public class PageView<T> { /** 分页数据 **/ private List<T> records; /** 页码开始索引和结束索引 **/ private PageIndex pageindex; /** 总页数 **/ private long totalpage = 1; /** 每页显示记录数 **/ private int maxresult = 12; /** 当前页 **/ private int currentpage = 1; /** 总记录数 **/ private long totalrecord; /** 页码数量 **/ private int pagecode = 10; /** 要获取记录的开始索引 **/ public int getFirstResult() { return (this.currentpage-1) * this.maxresult; } public int getPagecode() { return pagecode; } public void setPagecode(int pagecode) { this.pagecode = pagecode; } public PageView(int maxresult, int currentpage) { this.maxresult = maxresult; this.currentpage = currentpage; } public void setQueryResult(QueryResult<T> qr){ setTotalrecord(qr.getTotal()); setRecords(qr.getResultList()); } public long getTotalrecord() { return totalrecord; } public void setTotalrecord(long totalrecord) { this.totalrecord = totalrecord; setTotalpage(this.totalrecord % this.maxresult == 0 ? this.totalrecord / this.maxresult : this.totalrecord / this.maxresult + 1); } public List<T> getRecords() { return records; } public void setRecords(List<T> records) { this.records = records; } public PageIndex getPageindex() { return pageindex; } public long getTotalpage() { return totalpage; } public void setTotalpage(long totalpage) { this.totalpage = totalpage; this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage); } /** * 每页显示记录数 * @return */ public int getMaxresult() { return maxresult; } public int getCurrentpage() { return currentpage; } }
import java.util.List; /** * 分页显示对象 * @param <T> */ public class PageView<T> { /** 分页数据 **/ private List<T> records; /** 页码开始索引和结束索引 **/ private PageIndex pageindex; /** 总页数 **/ private long totalpage = 1; /** 每页显示记录数 **/ private int maxresult = 12; /** 当前页 **/ private int currentpage = 1; /** 总记录数 **/ private long totalrecord; /** 页码数量 **/ private int pagecode = 10; /** 要获取记录的开始索引 **/ public int getFirstResult() { return (this.currentpage-1) * this.maxresult; } public int getPagecode() { return pagecode; } public void setPagecode(int pagecode) { this.pagecode = pagecode; } public PageView(int maxresult, int currentpage) { this.maxresult = maxresult; this.currentpage = currentpage; } public void setQueryResult(QueryResult<T> qr){ setTotalrecord(qr.getTotal()); setRecords(qr.getResultList()); } public long getTotalrecord() { return totalrecord; } public void setTotalrecord(long totalrecord) { this.totalrecord = totalrecord; setTotalpage(this.totalrecord % this.maxresult == 0 ? this.totalrecord / this.maxresult : this.totalrecord / this.maxresult + 1); } public List<T> getRecords() { return records; } public void setRecords(List<T> records) { this.records = records; } public PageIndex getPageindex() { return pageindex; } public long getTotalpage() { return totalpage; } public void setTotalpage(long totalpage) { this.totalpage = totalpage; this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage); } /** * 每页显示记录数 * @return */ public int getMaxresult() { return maxresult; } public int getCurrentpage() { return currentpage; } }
另外一个
/** * 算出页码的开始索引和结束索引 */ public class PageIndex { /** 开始索引 */ private long startindex; /** 结束索引 */ private long endindex; public PageIndex(long startindex, long endindex) { this.startindex = startindex; this.endindex = endindex; } public long getStartindex() { return startindex; } public void setStartindex(long startindex) { this.startindex = startindex; } public long getEndindex() { return endindex; } public void setEndindex(long endindex) { this.endindex = endindex; } /** * 算出页码的开始索引和结束索引 * @param viewpagecount 页码数量 * @param currentPage 当前页数 * @param totalpage 总页数 * @return */ public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){ long startpage = currentPage - (viewpagecount % 2 == 0 ? viewpagecount / 2 - 1 : viewpagecount / 2); long endpage = currentPage + viewpagecount / 2; if(startpage < 1){ startpage = 1; if(totalpage >= viewpagecount) endpage = viewpagecount; else endpage = totalpage; } if(endpage > totalpage){ endpage = totalpage; if((endpage - viewpagecount)> 0) startpage = endpage - viewpagecount + 1; else startpage = 1; } return new PageIndex(startpage, endpage); }
/** * 算出页码的开始索引和结束索引 */ public class PageIndex { /** 开始索引 */ private long startindex; /** 结束索引 */ private long endindex; public PageIndex(long startindex, long endindex) { this.startindex = startindex; this.endindex = endindex; } public long getStartindex() { return startindex; } public void setStartindex(long startindex) { this.startindex = startindex; } public long getEndindex() { return endindex; } public void setEndindex(long endindex) { this.endindex = endindex; } /** * 算出页码的开始索引和结束索引 * @param viewpagecount 页码数量 * @param currentPage 当前页数 * @param totalpage 总页数 * @return */ public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){ long startpage = currentPage - (viewpagecount % 2 == 0 ? viewpagecount / 2 - 1 : viewpagecount / 2); long endpage = currentPage + viewpagecount / 2; if(startpage < 1){ startpage = 1; if(totalpage >= viewpagecount) endpage = viewpagecount; else endpage = totalpage; } if(endpage > totalpage){ endpage = totalpage; if((endpage - viewpagecount)> 0) startpage = endpage - viewpagecount + 1; else startpage = 1; } return new PageIndex(startpage, endpage); }
下面是Freemarker的分页宏
- <#-- 总页数,当前页 -->
- <#macro pagination pageView>
- <div class="pages">
- <label>共${pageView.totalpage}页,约${pageView.totalrecord}条数据</label>
- <#if pageView.currentpage != 1>
- <a href="javascript:pageinationView(1)" title="首页" class="nav"><span>首页</span></a>
- <a href="javascript:pageinationView(${pageView.currentpage - 1})" title="上一页" class="nav"><span>上一页</span></a>
- <#else>
- <span>首页</span>
- <span>上一页</span>
- </#if>
- <#list pageView.pageindex.startindex..pageView.pageindex.endindex as index>
- <#if pageView.currentpage == index>
- <a href="#" class="current">${index}</a>
- <#else>
- <a href="javascript:pageinationView(${index})" title="第${index}页" >${index}</a>
- </#if>
- </#list>
- <#if pageView.currentpage != pageView.totalpage>
- <a href="javascript:pageinationView(${pageView.currentpage + 1})" title="下一页" class="nav"><span>下一页</span></a>
- <a href="javascript:pageinationView(${pageView.totalpage})" title="未页" class="nav"><span>未页</span></a>
- <#else>
- <span>下一页</span>
- <span>未页</span>
- </#if>
- </div>
- <script type="text/javascript">
- function pageinationView(pageNum) {
- document.getElementById("pageNum").value=pageNum;
- document.getElementById("pageinationForm").submit();
- }
- </script>
- </#macro>
<#-- 总页数,当前页 --> <#macro pagination pageView> <div class="pages"> <label>共${pageView.totalpage}页,约${pageView.totalrecord}条数据</label> <#if pageView.currentpage != 1> <a href="javascript:pageinationView(1)" title="首页" class="nav"><span>首页</span></a> <a href="javascript:pageinationView(${pageView.currentpage - 1})" title="上一页" class="nav"><span>上一页</span></a> <#else> <span>首页</span> <span>上一页</span> </#if> <#list pageView.pageindex.startindex..pageView.pageindex.endindex as index> <#if pageView.currentpage == index> <a href="#" class="current">${index}</a> <#else> <a href="javascript:pageinationView(${index})" title="第${index}页" >${index}</a> </#if> </#list> <#if pageView.currentpage != pageView.totalpage> <a href="javascript:pageinationView(${pageView.currentpage + 1})" title="下一页" class="nav"><span>下一页</span></a> <a href="javascript:pageinationView(${pageView.totalpage})" title="未页" class="nav"><span>未页</span></a> <#else> <span>下一页</span> <span>未页</span> </#if> </div> <script type="text/javascript"> function pageinationView(pageNum) { document.getElementById("pageNum").value=pageNum; document.getElementById("pageinationForm").submit(); } </script> </#macro>
页面的引用
- <!--分页 -->
- <form id="pageinationForm" method="post" action="clazz-list">
- <input type="hidden" id="pageNum" name="pageNum" value="${pageNum}" />
- </form>
- <#import "*/main/main-page.ftl" as pager>
- <@pager.pagination pageViewpageView=pageView/>
- <!-- 分页 -->
<!--分页 --> <form id="pageinationForm" method="post" action="clazz-list"> <input type="hidden" id="pageNum" name="pageNum" value="${pageNum}" /> </form> <#import "*/main/main-page.ftl" as pager> <@pager.pagination pageView=pageView/> <!-- 分页 -->
现在应该出来了基本的分页。
相关推荐
81314797 2020-11-18
89314493 2020-11-03
81941231 2020-09-17
thisisid 2020-09-09
如狼 2020-08-15
82384399 2020-06-16
86384798 2020-05-12
80183053 2020-05-02
86384798 2020-04-26
86384798 2020-04-11
rionchen 2020-04-09
86384798 2020-04-07
86384798 2020-04-04
80183053 2020-03-07
87201943 2020-03-06
83961233 2020-02-26
87201943 2020-02-21