分页查询功能_代码实现_后台代码实现与分页查询功能_代码实现_前台代码实现

分页查询功能_代码实现_后台代码实现

package hf.xueqiang.web.servlet;

import hf.xueqiang.domain.PageBean;
import hf.xueqiang.domain.User;
import hf.xueqiang.service.UserService;
import hf.xueqiang.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           //1.获取参数
        String currentPage = request.getParameter("currentPage");//当前页嘛
        String rows = request.getParameter("rows");//每页显示条数
           //2.调用Servlet查询
        UserService service = new UserServiceImpl();
         PageBean<User> pb =  service.findUserByPage(currentPage,rows);

        System.out.println(pb);

           //3.将PageBean存入 request
         request.setAttribute("pb",pb);
           //4.转发到list.jsp
         request.getRequestDispatcher("list.jsp").forward(request,response);



    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

 

 @Override
    public PageBean<User> findUserByPage(String _currentPage, String _rows) {

        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        //1.创建空的PageBean对象
        PageBean<User> pb = new PageBean<User>();
        //2.设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3.调用dao查询总记录数
        int totalCount = dao.findTotalCount();
        pb.setTotalPage(totalCount);
        //4.调用dao查询list集合
        //计算开始的记录索引
        int start =(currentPage - 1)* rows;
        List<User> list =  dao.findByPage(start,rows);
        pb.setList(list);
        //5.计算总页码
        int totalPage = totalCount % rows  ==0 ? totalCount/rows : totalCount/rows +1;
        pb.setTotalPage(totalPage);
        return pb;
    }

 

  @Override
    public int findTotalCount() {
        String sql = "select count(*) from user";
            return template.queryForObject(sql,Integer.class);
    }

    @Override
    public List<User> findByPage(int start, int rows) {
        String  sql ="select * from user limit ?,?";
        return template.query(sql,new BeanPropertyRowMapper<User>(User.class),start,rows);
    }

 

    

分页查询功能_代码实现_前台代码实现 

 

 

 

 

 

 

    

 

 

 解决方法:  

    

 if (currentPage ==null || "".equals(currentPage)){
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)){
            rows = "5";
        }

 

 <nav aria-label="Page navigation">
            <ul class="pagination">
                <li>
                    <a href="#" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                <c:forEach begin="1" end="${pb.totalPage}" var="i">
                    <c:if test="${pb.currentPage == i}">
                        <li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5">${i}</a></li>
                    </c:if>
                    <c:if test="${pb.currentPage != i}">
                        <li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5">${i}</a></li>
                    </c:if>
                </c:forEach>
                <li>
                    <a href="#" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                <span style="font-size: 25px;margin-left: 5px;">
                    共${pb.totalCount}条记录,共${pb.totalPage}页
                </span>
            </ul>
        </nav>

 

  

 

 

 上一页:

  <li>
                    <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage -  1}&rows=5" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>

下一页:

 <li>
                    <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage + 1}&rows=5" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>

 

 

 

 

 

 

 

 

 解决办法:

   

       <c:if test="${pb.currentPage == 1}">
                    <li class="disabled">
                </c:if>>
                    <c:if test="${pb.currentPage != 1}">
                    <li>
                        </c:if>>

 

 if (currentPage <=0){
            currentPage= 1;
        }

 

 

<li>
<a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage + 1}&rows=5" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
posted @ 2023-02-15 13:08  zj勇敢飞,xx永相随  阅读(44)  评论(0编辑  收藏  举报