freemarker分页,首先在web.xml中做好配置(这里说的配置就是让web过滤我们的freemarker指令)。
下面是代码:
<#assign currentPage=(RequestParameters["currentPage"]?number)!1>//传入的当前页,如果没有传入,默认就是第一页。
<#assign count=reply.count>//这里的reply.count是程序中用到的一个参数,也是从其他页面传过来的“总记录数”。
<#assign next=(currentPage+1)>
<#assign last=(currentPage-1)>
<#if count%10==0>
<#assign page=count/10>
<#else>
<#assign page=(count/10)?int+1>
</#if>
<#if (currentPage-3>1)>
<#assign first=(currentPage-3)>
<#else>
<#assign first=1>
</#if>
<#if ((currentPage+3)>page) >
<#assign end=page>
<#else>
<#assign end=(currentPage+3)>
</#if>
<#assign f=first>
<#macro loop>
<#if (f>end) >
<#else>
<#if currentPage==f>
<span class=tP>${f} </span>
<#else>
<a href="replylist.html?msgid=${msgid}¤tPage=${f}">${f}</a> //分页的页面。
</#if>
<#assign f=f+1>
<@loop />
</#if>
</#macro>
<#macro firstP>
<#if currentPage!=1>
<a href="replylist.html?msgid=${msgid}¤tPage=1">首页</a><a href="replylist.html?msgid=${msgid}¤tPage=${last}">上一页</a>
</#if>
</#macro>
<#macro endP>
<#if currentPage!=page>
<a href="replylist.html?msgid=${msgid}¤tPage=${next}">下一页</a><a href="replylist.html?msgid=${msgid}¤tPage=${page}">尾页</a>
</#if>
</#macro>//宏定义结束
<@firstP /><@loop /><@endP />//循环
本文介绍如何使用Freemarker实现动态网页分页功能。通过设置当前页、总记录数等参数,计算出分页所需的页码范围,并利用宏定义展示页码链接,实现了前后翻页及跳转至指定页的功能。
295

被折叠的 条评论
为什么被折叠?



