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<Deal> 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<T> 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>

<div class="page">
<#--第一页,禁用“上一页”按钮-->
    <#if pagingList.total == 0 || pagingList.page == 1>
        <span class="prev-disabled">上一页</span>
    <#else>
        <#if pagingList.page == 2>
            <a href="${url}">上一页</a>
        <#else>
            <a href="${url}/${pagingList.page-1}">上一页</a>
        </#if>
    </#if>

<#--第一页-->
    <#if (pagingList.total > 0)>
        <a href="${url}" <#if pagingList.page == 1>class="current_page"</#if>>1</a>
    </#if>

<#--如果不只有一页-->
    <#if (maxPageIndex > 1)>
    <#--如果当前页往前查3页不是第2页-->
        <#if ((pagingList.page - 3) > 2)>
            <span class="text">…</span>
        </#if>

    <#--当前页的前3页和后3页-->
        <#list (pagingList.page - 3)..(pagingList.page + 3) as index>
        <#--如果位于第一页和最后一页之间-->
            <#if (index > 1) && (index < maxPageIndex)>
                <a href="${url}/${index}" <#if pagingList.page == index>class="current_page"</#if>>${index}</a>
            </#if>
        </#list>

    <#--如果当前页往后查3页不是倒数第2页-->
        <#if (pagingList.page + 3) < (maxPageIndex - 1)>
            <span class="text">…</span>
        </#if>

    <#--最后页-->
        <a href="${url}/${maxPageIndex}" <#if pagingList.page == maxPageIndex>class="current_page"</#if>>${maxPageIndex}</a>
    </#if>

<#--最后页,禁用“下一页”按钮-->
    <#if pagingList.total == 0 || pagingList.page == maxPageIndex>
        <span class="prev-disabled">下一页</span>
    <#else>
        <a href="${url}/${pagingList.page+1}">下一页</a>
    </#if>
</div>
</#macro>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值