Some knowledge and application on front and back pages of

        We know that the data displayed in the foreground is generally spread information about the past, some list collection package, but the face of numerous natural data can not be displayed in one page is completed,

We need paging process. It should be front and back, respectively, and data processing and interactive page on the line in order to form a good interface.

      Background code start talking, first reception data transmitted more than a set number of parameters further paged data, so here we choose a package PageBean, generally only five data, a case of selecting to add detail below

       // this page 
    Private  int The currentPage;
     // the Number of this page displayed 
    Private  int the currentCount;
     // total number of pieces 
    Private  int The totalCount;
     // Pages 
    Private  int TotalPage;
     // Data Showing the 
    Private List <T> = List new new the ArrayList <T> ();

currentPage this page representing the page visits this page, the number of the current page displayed currentCount, The totalCount the total number of pieces, the total number of pages TotalPage (can be obtained by calculation) page list data representative of generic reusable tab defined as use.

String cpg = request.getParameter("currentPage");
        if(cpg==null) cpg="1";
        
        int currentPage=Integer.parseInt(cpg);
        int currentCount=10;
        PageBean<Person> pageBean= ps.findPageBean(currentPage,currentCount);
        
        request.setAttribute("pagebean", pageBean);

Because the current page is the first visit by default the first page, so once a judge assigned to 1, where I write data per page to 10, as the case may set their own, then that is by dao database query data from the paging service encapsulation layer. And save it in the object domain can forward or redirect.

The following data is encapsulated into objects

PageBean = PageBean new new PageBean (); 
        the PersonDao PD = new new PersonDaoImpl ();
         // 1. this page 
        pageBean.setCurrentPage (The currentPage);
         // 2. Article this page number 
        pageBean.setCurrentCount (the currentCount);
         // 3. Total Article number 
        int The totalCount = pd.getTotalCount (); 
        pageBean.setTotalCount (The totalCount); 
        // 4. pages 
        int TotalPage = ( int ) Math.ceil (1.0 * The totalCount / the currentCount); 
        pageBean.setTotalPage (TotalPage); 
        // the page data 
        int index = (currentPage - 1) * currentCount;
        List<Person> list = pd.findPersonListForPageBean(index, currentCount);
        pageBean.setList(list);

The total number of pages and list of data need to be a database query operations are directly assigned or other computing available.

Attach dao data

@Override
    public int getTotalCount() {
        // TODO Auto-generated method stub
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select count(*) from user";
        long query = 0;
        try {
            query = (long) runner.query(sql, new ScalarHandler());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (int) query;
    }

It should be noted here check out the total number of default type is long, you need to type into int own.

@Override
    public List<Person> findPersonListForPageBean(int index, int currentCount) {
        // TODO Auto-generated method stub
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select *from user limit ?,?";
        List<Person> query = null;
        try {
            query = runner.query(sql, new BeanListHandler<Person>(Person.class), index, currentCount);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return query;
    }

This is normal paging operations.

At this point, some of the pages have all the background ready to prepare complete, the data sent to the front, that is to the front desk the remainder of the process.

 Reception related

<div class="col-md-12">
        <div class="text-center">
            <ul class="pagination">
            <c:if test="${pagebean.currentPage==1 }">
            <li class="disabled"><a href="javascript:void(0);">&laquo;</a></li>
            </c:if>
            <c:if test="${pagebean.currentPage!=1 }">
            <li><a href="${pageContext.request.contextPath}/personlist?currentPage=${pagebean.currentPage-1}">&laquo;</a></li>
            </c:if>
            
            
            <c:forEach begin="1" end="${pagebean.totalPage }" var="page">
            <c:if test="${pagebean.currentPage==page }">
            <li class="active"><a href="javascript:void(0);">${page }</a></li>            
            </c:if>
            <c:if test="${pagebean.currentPage!=page }">
            <li><a href="${pageContext.request.contextPath}/personlist?currentPage=${page}">${page }</a></li>            
            </c:if>
            </c:forEach>
            
            
            <c:if test="${pagebean.currentPage==pagebean.totalPage }">
            <li class="disabled"><a href="javascript:void(0);">&raquo;</a></li>
            </c:if>
            <c:if test="${pagebean.currentPage!=pagebean.totalPage }">
            <li><a href="${pageContext.request.contextPath}/personlist?currentPage=${pagebean.currentPage+1}">&raquo;</a></li>
            </c:if>
            </ul>
        </div>
    </div>

Here are some Front page article, the use of template style bootstrap

 

 The reception is some logic to note: at the time of the first page can not click on the previous page, the last page of the same can not access the next page. Click on this page can not jump, when in need dark current page to show the difference to other pages.

And if combined with some of the foreach statement jstl to achieve the above functions,

javascript: void (0) behalf not jump that is not clickable.

Some other studies to their own logic.

 

Guess you like

Origin www.cnblogs.com/lin530/p/11594659.html