Basic problem-solving ideas paging query

  The first step: We first determine how much data to put each page, a total of how much data, how many pages to be divided, for the previous and next, Home, End number of pages analysis, which according to create tools page, page accept the current number of pages web page returned (if no data is by default a number of pages), returns the correct Next ... of, servlet achieve the correct number of pages by calling the page query tools. Finally, the data is returned to the jsp page display.

  So Tools:

com.person.util Package; 

public  class PageUtil {
     Private Integer The currentPage; // this page 
    Private Integer firstPage; // First 
    Private Integer lastPage; // End 
    Private Integer nextPage; // Next 
    Private Integer proPage; / / Previous 

    Private Integer TotalPage; // pages 
    Private Integer the totalCount; // total data amount 

    Private Integer index; // index tab query 

    Private PageUtil () {} 

    public PageUtil(String currentPage,Integer totalCount){
        this.currentPage=currentPage==null? 1: Integer.parseInt(currentPage);
        this.totalPage=(totalCount+4)/5;

        this.firstPage = 1;
        this.lastPage = this.totalPage;

        this.nextPage = this.currentPage==this.lastPage ? this.lastPage : this.currentPage+1;
        this.proPage= this.currentPage==1?1:this.currentPage-1;
        this.totalCount=totalCount;
        this.index=(this.currentPage-1)*5;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getFirstPage() {
        return firstPage;
    }

    public void setFirstPage(Integer firstPage) {
        this.firstPage = firstPage;
    }

    public Integer getLastPage() {
        return lastPage;
    }

    public void setLastPage(Integer lastPage) {
        this.lastPage = lastPage;
    }

    public Integer getNextPage() {
        return nextPage;
    }

    public void setNextPage(Integer nextPage) {
        this.nextPage = nextPage;
    }

    public Integer getProPage() {
        return proPage;
    }

    public void setProPage(Integer proPage) {
        this.proPage = proPage;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    @Override
    public String toString() {
        return "PageUtil{" +
                "currentPage=" + currentPage +
                ", firstPage=" + firstPage +
                ", lastPage=" + lastPage +
                ", nextPage=" + nextPage +
                ", proPage=" + proPage +
                ", totalPage=" + totalPage +
                ", totalCount=" + totalCount +
                ", index=" + index +
                '}';
    }
}

Then be prepared by the DAO:

interface:

 / * * 
     * Query for information according to a specific fixed number of Page (pages) 
     * @param index 
     * @return 
     * / 
    List <the Map <String, Object >> getAdminByIndex (Integer index);

impl:

 @Override
    public List<Map<String, Object>> getAdminByIndex(Integer index) {
        String sql="select * from administrators limit ?,5";
        return DBUtil.executeQuery(sql,index);
    }

 

Preparatory work carried out after the completion of writing a servlet:

 

package com.person.servlet;

import com.person.dao.IAdminDAO;
import com.person.dao.impl.AdminDAOImpl;
import com.person.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/dromAdminHome.isLogin")
public class DromAdminHomeServlet  extends HttpServlet {
    @Override
    protected void-Service (the HttpServletRequest REQ, the HttpServletResponse RESP) throws ServletException, IOException {
         // This information is passed over the page of the page 
        String = req.getParameter page ( " page " );
         // Set the page information into the tools, 
        PageUtil pageUtil = new new PageUtil ( page, 7 ); 
        
        // query the information on this page: 
        IAdminDAO adminDAO = new new AdminDAOImpl (); 
        List <the Map <String, Object >> allAdmin = adminDAO.getAdminByIndex (pageUtil.getIndex ()); 
        
        // to check out the information set in the req clickable while other pages of information tools are also calculated in the deposit req
         // so if the implementation of this servlet to click, then click on the page will calculate the outer page of other pages the information in this page is displayed query
        req.setAttribute("allAdmin",allAdmin);
        req.setAttribute("pageData",pageUtil);

        req.getRequestDispatcher("/views/dromAdminHome.jsp").forward(req,resp);

    }
}

 

 

 

Finally, write jsp page:

 

<the Table cellspacing = " 0px " > 
        <TR> 
            <td> ID </ td> 
            <td> Name </ td> 
            <td> gender </ td> 
            <td> Phone </ td> 
            <td> dormitory </ TD> 
            <TD> username </ TD> 
            <TD> operation </ TD> 
        </ TR> 
        <C: forEach var = " A " items = " $ {allAdmin} " > 
            <TR> 
                <TD> $ {A } .id </ TD> 
                <TD> a.name $ {} </ TD>
                <td>${a.gender}</td>
                <td>${a.telephone}</td>
                <td>${a.dormitory}</td>
                <td>${a.username}</td>
                <td><a href="/yuer/views/updateAdmin.jsp">修改</a> <a href="/yuer/deleteAdmin?id=${a.id}">删除</a></td>
            </tr>
        </c:forEach>

    </table>
    <div style="text-align: center">
        <a  href="/yuer/dromAdminHome.isLogin?page=${pageData.firstPage}" >首页</a>
        <a  href="/yuer/dromAdminHome.isLogin?page=${pageData.proPage}""
        <A the href => Previous </a> /Yuer/dromAdminHome.isLogin?page=${pageData.nextPage} " > Next </a> 
        <a href= "/yuer/dromAdminHome.isLogin?page=${pageData.lastPage}"> End < / a> 
        co pageData.currentPage {} {$ /${pageData.totalPage per page]} [ 5 ] Article
     </ div>

 

 

 

over~~~~~~~~~~~

Guess you like

Origin www.cnblogs.com/fanqiexin/p/11105618.html