springmvc中分頁

此處爲Page類

package com.howin.pojo;

import java.util.List;

public class Page<E> {
    // 結果集
    private List<E> list;

    // 查詢記錄總數
    private int totalRecords;

    // 每頁多少條記錄
    private int pageSize;

    // 第幾頁
    private int pageNo;
    
    /**
     * @return 總頁數
     * */
    public int getTotalPages(){
        if ((totalRecords+pageSize-1)/pageSize==0) {
            return 1;
        } else {
            return (totalRecords+pageSize-1)/pageSize;
        }
        
    }
  
 計算當前頁開始記錄
    public int countOffset(int currentPage,int pageSize){
        int offset = pageSize*(currentPage-1);
        return offset;
    }
    
    /**
     * @return 首頁
     * */
    public int getTopPageNo(){
        return 1;
    }
    
    /**
     * @return 上一頁
     * */
    public int getPreviousPageNo(){
        if(pageNo<=1){
            return 1;
        }
        return pageNo-1;
    }
    
    /**
     * @return 下一頁
     * */
    public int getNextPageNo(){
        if(pageNo>=getBottomPageNo()){
            return getBottomPageNo();
        }
        return pageNo+1;
    }
    
    /**
     * @return 尾頁
     * */
    public int getBottomPageNo(){
        return getTotalPages();
    }
    
    
    public List<E> getList() {
        return list;
    }

    public void setList(List<E> list) {
        this.list = list;
    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }
}


在BizImpl層

@Override
    public Page queryallKnowledge(int currentPage,int pageSize) {
        Page page = new Page();        
        //總記錄數
      int allRow= knowledgeDao.getAllrowCount();
        //當前頁開始記錄
        int offset = page.countOffset(currentPage,pageSize);  
        //分頁查詢結果集
        List<Knowledge> list =knowledgeDao.queryallKnowledge(offset, pageSize);

        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        
        return page;
    }

在DaoImpl層

@Override
    public List<Knowledge> queryallKnowledge(int offset,int pageSize) {
        @SuppressWarnings("unchecked")
        Query query= sessionFactory.getCurrentSession().createQuery("from Knowledge");
        query.setFirstResult(offset);
        query.setMaxResults(pageSize);
        return query.list();
    }

在Controller層

// 根據用戶ID查詢知識
    @RequestMapping("/knowledgeList")
    public String queryKnowledgeByUserId(Map<String, Object> model, Integer userid, HttpServletRequest request,String pageNo) {
        List<Knowledge> knowledgeList = new ArrayList<Knowledge>();
        //定義Page類
                Page page;
                //pageNo是前臺傳過來的當前頁,如果是第一次,默認顯示第一頁
                if(pageNo==null){
                    pageNo="1";
                }
                //根據當前頁,每次查詢5條
                page=knowledgeBiz.queryPageKonwledgeByUserId(userid,Integer.valueOf(pageNo),5);
                knowledgeList.addAll(page.getList());
        model.put("page", page);
        model.put("knowledgeList", knowledgeList);
        request.setAttribute("knowledgeListSize", knowledgeList.size());
        return "user/knowledgeList";
    }

在前臺jsp中

<div id="btn_m">
            <a>共${page.totalRecords}條記錄 共${page.totalPages}頁 當前第${page.pageNo}頁
                
                <a href="${pageContext.request.contextPath}/user/knowledgeList?pageNo=${page.getTopPageNo()}&&userid=${sessionScope.user.id}"><input type="button" name="fristPage" value="首頁" class="btn btn-info"/></a>
                <c:choose>
                  <c:when test="${page.pageNo!=1}">
                    
                      <a href="${pageContext.request.contextPath}/user/knowledgeList?pageNo=${page.previousPageNo }&&userid=${sessionScope.user.id}"><input type="button" name="previousPage" value="上一頁" class="btn btn-info"/></a>
                  </c:when>
                  <c:otherwise>
                    
                      <input type="button" disabled="disabled" name="previousPage" value="上一頁" class="btn btn-info"/>
                    
                  </c:otherwise>
                </c:choose>
                <c:choose>
                  <c:when test="${page.pageNo != page.totalPages}">
                    <a href="${pageContext.request.contextPath}/user/knowledgeList?pageNo=${page.getNextPageNo() }&&userid=${sessionScope.user.id}"><input type="button" name="nextPage" value="下一頁" class="btn btn-info"/></a>
                  </c:when>
                  <c:otherwise>
                    
                      <input type="button" disabled="disabled" name="nextPage" value="下一頁" />
                    
                  </c:otherwise>
                </c:choose>
                
                <a href="${pageContext.request.contextPath}/user/knowledgeList?pageNo=${page.bottomPageNo }&&userid=${sessionScope.user.id}"><input type="button" name="lastPage" value="尾頁" class="btn btn-info"/></a>
              <a id="ahref" href="#" οnclick="location.href='${pageContext.request.contextPath}/user/knowledgeList?pageNo='+document.getElementById('indexnum').value.trim()+'&&userid=${sessionScope.user.id}';"><input type="button" value="點此跳轉" class="btn btn-info" id="btn"  οnclick="test()"/></a><input id="indexnum" type="text" />
            </div>

<script>
function test(){
var num=document.getElementById("indexnum").value.trim();
if(num==""){
    document.getElementById("indexnum").value="";
    alert("請輸入內容!!");
    document.getElementById("ahref").οnclick="";
    window.location.reload();
}else if(isNaN(num)){
    document.getElementById("indexnum").value="";
    alert("只能輸入數字!");
    document.getElementById("ahref").οnclick="";
    window.location.reload();
}else if(num>${page.totalPages}  || num<=0){
    document.getElementById("indexnum").value="";
    alert("請輸入符合規範的頁碼!!");
    document.getElementById("ahref").οnclick="";
    window.location.reload();
}
}
</script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章