關於分頁前台頁面+后台實現


后台分頁工具類

package com.entity;

/**
 * 分頁工具類
 * 
 * @author H.ros
 *
 */
public class PageUtils {
    // 當前頁默認第1頁(從頁面獲取的當前頁碼,未計算)
    private int currentPage = 1;
    // 前一頁
    private int prevPage;
    // 下一頁
    private int nextPage;
    // 尾頁
    private int lastPage;
    // 總記錄數
    private int count;
    // 每頁的條數,默認10條
    private int pageSize = 1;
    // 開始記錄數,分頁計入數(使用時調用的初始頁,計算后)
    private int pageRecord;
    // 頁面分頁html模型代碼
    private String page;

    // 有參構造器
    public PageUtils(String currentPage, int count, String pageSize) {
        init(currentPage, count, pageSize);
        initLastPage();
        initCurrentPage();
        initPrevPage();
        initNextPage();
        initPageRecord();
        initPage();
    }

    // 初始化三個重要元素
    private void init(String currentPage, int count, String pageSize) {
        if (currentPage != null && currentPage != "" && currentPage != "0") {
            this.currentPage = Integer.parseInt(currentPage);
        }

        this.count = count;

        if (pageSize != null && pageSize != "" && pageSize != "0") {
            this.pageSize = Integer.parseInt(pageSize);
            ;
        }
    }

    // 初始化尾頁
    private void initLastPage() {
        if (count % pageSize == 0) {
            lastPage = count / pageSize;
        } else {
            lastPage = count / pageSize + 1;
        }
    }

    // 初始化並矯正當前頁(防止外部訪問出錯)
    private void initCurrentPage() {
        if (currentPage < 1) {
            currentPage = 1;
        } else if (currentPage > lastPage) {
            currentPage = lastPage;
        }
    }

    // 初始化上一頁
    private void initPrevPage() {
        if (currentPage != 1) {
            prevPage = currentPage - 1;
        } else {
            prevPage = 1;
        }
    }

    // 初始化下一頁
    private void initNextPage() {
        if (currentPage != lastPage) {
            nextPage = currentPage + 1;
        } else {
            nextPage = lastPage;
        }
    }

    // 初始化分頁計入數
    private void initPageRecord() {
        pageRecord = (currentPage - 1) * pageSize;
        if (pageRecord < 0) {
            pageRecord = 0;
        }
    }

    // 初始化html頁面分頁模型代碼
    private void initPage() {
        page = "<div style='text-align: right;'>";
        page += "第" + currentPage + "/" + lastPage + "頁,共" + count + "條記錄。";
        page += "<select style='width: 70px;' id='pageSize' onchange='page(1)'><option>1</option><option>2</option><option>5</option></select>";
        page += "<input type='button' value='首頁' onclick='page(1)' />";
        page += "<input type='button' value='上一頁' onclick='page(" + prevPage + ")' />";
        page += "<input type='button' value='下一頁' onclick='page(" + nextPage + ")' />";
        page += "<input type='button' value='尾頁' onclick='page(" + lastPage + ")' />";
        page += "</div>";
    }

    /*
     * 對外訪問通道
     */
    public int getCurrentPage() {
        return currentPage;
    }

    public int getPrevPage() {
        return prevPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public int getLastPage() {
        return lastPage;
    }

    public int getCount() {
        return count;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getPageRecord() {
        return pageRecord;
    }

    public String getPage() {
        return page;
    }

}

后台代碼

查詢語句中
    int count = bannerService.selectCount();
        PageUtils pageUtils = new PageUtils(currentPage, count, pageSize);
        // 開始記錄數,由工具類中計算得出
        int begin = pageUtils.getPageRecord();
        // 查幾條記錄
        int number = pageUtils.getPageSize();
        // 前台頁面分頁模型(傳入頁面使用的html代碼)
        String page = pageUtils.getPage();
        List<Banner2> list = bannerService.selectAll(begin, number);

        map.put("banner", list);
        map.put("page", page);
        map.put("number", number);

dao。xml層

 <select id="selectAll" resultMap="BaseResultMap" >
 
    select * from banner limit ${begin},${number}
  </select>
  
  <select id="selectCount" resultType="int">
  select count(*) count from banner
  </select>

前台頁面撰寫

$(function(){
    $("#pageSize").val("${number}");
})
function page(i){
    //獲取到下拉框選中的值
    var pageSize = $("#pageSize").find("option:selected").text()
    window.location.href="/ShoppingBackstage/banner/indexs?currentPage="+i+"&pageSize="+pageSize+" "
}

${page}

 實現層

public interface bannerService {
    public List<Banner2> selectAll(int begin, int number);

    public int selectCount();

    public void insert(Banner2 banner);

    public void delete(int id);

    public void update(Banner2 banner2);

    public Banner2 selectByid(int id);

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM