欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java web velocity分页宏示例

程序员文章站 2024-02-27 13:23:33
复制代码 代码如下:#macro(pager $url $pager)
     #set($front_len...

复制代码 代码如下:

#macro(pager $url $pager)
<ul class="pagination">
 #set($front_len = 4)
 #set($behind_len = 5)
 #set($pager_len = 10)
 #set($pager_center = $!{pager.pagenum} - $!{behind_len})
 <li #if($!{pager.currentpage} == 1) class="disabled"#end>
  <a href="$!{url}?page=1">«</a>
 </li>
 #if($!{pager.pagenum} <= $!{pager_len})
  #foreach($p in [1..$!{pager.pagenum}])
   <li #if($!{pager.currentpage} == $!{p}) class="active" #end>
    <a href="$!{url}?page=$!{p}">$!{p}</a>
   </li>
  #end
 #elseif($!{pager.pagenum} > $!{pager_len})
  #if($!{pager.currentpage} <= $!{front_len})
   #foreach($p in [1..10])
    <li #if($!{pager.currentpage} == $!{p}) class="active" #end>
     <a href="$!{url}?page=$!{p}">$!{p}</a>
    </li>
   #end
  #elseif($!{pager.currentpage} < $!{pager_center})
   #set($header = $!{pager.currentpage} - $!{front_len})
   #set($ender = $!{pager.currentpage} + $!{behind_len})
   #foreach($p in [$!{header}..$!{ender}])
    <li #if($!{pager.currentpage} == $!{p}) class="active" #end>
     <a href="$!{url}?page=$!{p}">$!{p}</a>
    </li>
   #end
  #else
   #set($begin = $!{pager.pagenum} - $!{front_len} - $!{behind_len})
   #foreach($p in [$!{begin}..$!{pager.pagenum}])
    <li #if($!{pager.currentpage} == $!{p}) class="active" #end>
     <a href="$!{url}?page=$!{p}">$!{p}</a>
    </li>
   #end
  #end
 #end
 <li #if($!{pager.currentpage} == $!{pager.pagenum}) class="disabled"#end>
  <a href="$!{url}?page=$!{pager.pagenum}">»</a>
 </li>
</ul>
#end