Java web velocity分页宏示例

时间:2022-06-15 18:04:36

代码如下:


#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