【JavaWeb】分页操作

导读:本篇文章讲解 【JavaWeb】分页操作,文章出自:https://blog.csdn.net/weixin_43316702/article/details/104807614希望对大家有帮助,欢迎收藏,转发!站点地址:www.javazhiyin.com.com

>>强大,10k+点赞的 SpringBoot 后台管理系统竟然出了详细教程!

分页原理:
javaee的分页基本都是在数据库中先进行分页后再将数据拿出来进行显示的。

//**UserDao**
//在数据库对学生按照一定数量进行分页(此处是用c3p0)
    public List<user> DisUserByPage(int page,int pageSize){
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSourse());
        String sql = "select * from student limit ?,?";
        int start = (page-1)*pageSize;
        int length = pageSize;
        try {
           return runner.query(sql,new BeanListHandler<user>( user.class),start,length);
        }catch (SQLException e){
            e.printStackTrace();
        }return null;
    }

    //获取学生数量(此处是使用jdbc)
    public int ucountJDBC(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int count = 0;
        try{
            conn = JDBCUtil.getConnection();
            String sql = "select count(*) from student";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                int coun = rs.getInt(1);
                return coun;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,pstmt,null);
        }return count;
    }
//**UserService**
UserDao userDao = new UserDao();
    //进行数据分页打包
    public pageresult<user> DisUserByPageService(int page,int pageSize){
        try{
            //创建模型
            pageresult<user> pr = new pageresult<>();
            //设置总数量
            int totalCount = userDao.ucountJDBC();
            pr.setTotalCount(totalCount);
            //设置页总数
            int totalPage = (int)Math.ceil(totalCount * 1.0 / pageSize);
            pr.setTotalPage(totalPage);
            //设置当前页
            pr.setCurrentPage(page);
            //设置数据集合 userList
            List<user> userList = userDao.DisUserByPage(page,pageSize);
            pr.setList(userList);
            return pr;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    //需要分页用户的总数量
    public int count() {
        return userDao.ucountJDBC();
    }
//**DisUserByPageServlet**
import com.Boke.domian.pageresult;
import com.Boke.domian.user;
import com.Boke.service.UserService;

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;

@WebServlet("/DisUserByPageServlet")
public class DisUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String pageStr = request.getParameter("page").trim();
        UserService us = new UserService();
        int count = us.count();
        int page = 1;//当前页数
        int pageSize = 5;//每页五个
        if(pageStr != null && !"".equals(pageStr)){
            //把字符串转成int
            page = Integer.parseInt(pageStr);
        }
        pageresult<user> list = us.DisUserByPageService(page,pageSize);
        request.setAttribute("user",list);
        request.setAttribute("num",count);
        request.getRequestDispatcher("/DisUserByPage.jsp").forward(request,response);
    }

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

pageresultuser

import java.util.List;
public class pageresult<T> {
    private List<T> list;//集合
    private long totalCount;//总记录数
    private int totalPage;//总页数
    private int currentPage;//当前页
    private int pageCount;//每页显示的条数

    public List<T> getList() {
        return list;
    }

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

    public long getTotalCount() {
        return totalCount;
    }

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

    public int getTotalPage() {
        return totalPage;
    }

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

    public int getCurrentPage() {
        return currentPage;
    }

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

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    @Override
    public String toString() {
        return "pageresult{" +
                "totalCount=" + totalCount +
                ", totalPage=" + totalPage +
                ", currentPage=" + currentPage +
                ", pageCount=" + pageCount +
                '}';
    }


public class user {
    private String sno;
    private String psw;
    private String name;
    private String title;

    public String getSno() {
        return sno;
    }

    public void setSno(String sno) {
        this.sno = sno;
    }

    public String getPsw() {
        return psw;
    }

    public void setPsw(String psw) {
        this.psw = psw;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "user{" +
                "sno='" + sno + '\'' +
                ", password='" + psw + '\'' +
                '}';
    }

页面点击进入
主页进入分页显示
第一页第二页总结:
分页操作简单来说就是将数据库中的数据按照一定的个数进行分页,分页完成后再将每页的数据进行打包封装后显示到页面中。

注意:如果出现如下图问题:
在这里插入图片描述解决方法:
1.查看lib目录是否导入了jar包,commons-dbutils-1.6.jar
2.进行如下操作:
步骤一:
在这里插入图片描述
步骤二:
在这里插入图片描述问题就能解决啦!