freemarker分页模板

后端数据格式及获取详情参见文章
Mybatis实现分页

controller层部分代码

     /**
     * 分页显示某个类别下的商品
     */
    @RequestMapping(value = "/category/{urlName}/{page}",method = RequestMethod.GET)
    public String listDealsOfDealCategory(@PathVariable String urlName, @PathVariable Integer page, Model model,
                                        HttpServletRequest request){
        DealCategory dealCategory=dealCategoryService.getByUrlName(urlName);
        model.addAttribute("dealCategory",dealCategory);
        PagingResult pagingResult=dealService.getDealOfCategories(dealCategory.getSelfAndChildrenIds(),
                super.getAreaId(request),page, DealConstant.DEAL_NUM_PER_PAGE_IN_DEALS_OF_CATEGORY_PAGE);
        model.addAttribute("pagingDealList", pagingResult);
        return "/deal/category";
    }

其中PagingResult的结构为:

package com.tortuousroad.framework.common.page;

import com.tortuousroad.framework.base.entity.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.List;

/**
 * 分页查询结果
 * Created by asus on 2017/8/1.
 */
@AllArgsConstructor
@ToString
public class PagingResult<T extends BaseEntity> implements Serializable {

    private static final long serialVersionUID = 3739432000715879711L;
    /**
     * 当前页码
     */
    @Getter @Setter
    private int page;
    /**
     * 总记录数
     */
    @Getter @Setter
    private long total;
    /**
     * 每页记录数
     */
    @Getter @Setter
    private int pageSize;
    /**
     * 每页数据记录
     */
    @Getter @Setter
    private List rows;


}

category.ftl

<#if pagingDealList??>
        <@paging_macro.paging pagingList=pagingDealList url="${ctx}/category/${dealCategory.urlName}"/>
    #if>

paging.ftl

<#macro paging pagingList url>
<#--计算最大页码-->
    <#if pagingList.total % pagingList.pageSize == 0>
        <#assign maxPageIndex = pagingList.total / pagingList.pageSize>
    <#else>
        <#assign maxPageIndex = (pagingList.total / pagingList.pageSize)?floor + 1>
    #if>

class="page"> <#--第一页,禁用“上一页”按钮--> <#if pagingList.total == 0 || pagingList.page == 1> class="prev-disabled">上一页 <#else> <#if pagingList.page == 2> "${url}">上一页 <#else> "${url}/${pagingList.page-1}">上一页 #if> #if> <#--第一页--> <#if (pagingList.total > 0)> "${url}" <#if pagingList.page == 1>class="current_page"if>>1 #if> <#--如果不只有一页--> <#if (maxPageIndex > 1)> <#--如果当前页往前查3页不是第2页--> <#if ((pagingList.page - 3) > 2)> class="text">… #if> <#--当前页的前3页和后3页--> <#list (pagingList.page - 3)..(pagingList.page + 3) as index> <#--如果位于第一页和最后一页之间--> <#if (index > 1) && (index < maxPageIndex)> "${url}/${index}" <#if pagingList.page == index>class="current_page"if>>${index} #if> #list> <#--如果当前页往后查3页不是倒数第2页--> <#if (pagingList.page + 3) < (maxPageIndex - 1)> class="text">… #if> <#--最后页--> "${url}/${maxPageIndex}" <#if pagingList.page == maxPageIndex>class="current_page"if>>${maxPageIndex} #if> <#--最后页,禁用“下一页”按钮--> <#if pagingList.total == 0 || pagingList.page == maxPageIndex> class="prev-disabled">下一页 <#else> "${url}/${pagingList.page+1}">下一页 #if>
#macro>

你可能感兴趣的:(freemarker)