servlet+jsp实现页面分页功能

建立分页的实体类
/**

    1. 控制层开发
  • @author Jie.Yuan

*/
public class DemoServlet extends HttpServlet {

private IEmployeeService employeeService = new EmployeeService();

private String uri;

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    
    try {
        
        String currPage = request.getParameter("currentPage");
        
        if (currPage == null || "".equals(currPage.trim())){
            currPage = "1";     
        }
       
        int currentPage = Integer.parseInt(currPage);
        
        
        PageBean pageBean = new PageBean();
        pageBean.setCurrentPage(currentPage);
        
        
        employeeService.getAll(pageBean);   
        
       
        request.setAttribute("pageBean", pageBean);
        
        
        uri = "/WEB-INF/list.jsp";
    } catch (Exception e) {
        e.printStackTrace();  
       
        uri = "/error/error.jsp";
    }
    request.getRequestDispatcher(uri).forward(request, response);
    
}

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

}

控制层
/**

    1. 控制层开发
  • @author Jie.Yuan

*/
public class DemoServlet extends HttpServlet {

private IEmployeeService employeeService = new EmployeeService();

private String uri;

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    
    try {
        
        String currPage = request.getParameter("currentPage");
        
        if (currPage == null || "".equals(currPage.trim())){
            currPage = "1";     
        }
       
        int currentPage = Integer.parseInt(currPage);
        
       
        PageBean pageBean = new PageBean();
        pageBean.setCurrentPage(currentPage);
        
          
        employeeService.getAll(pageBean);    
        
        
        request.setAttribute("pageBean", pageBean);
        
        //5. 跳转 
        uri = "/WEB-INF/list.jsp";
    } catch (Exception e) {
        e.printStackTrace();  
        
        uri = "/error/error.jsp";
    }
    request.getRequestDispatcher(uri).forward(request, response);
    
}

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

}

数据访问:
/**

    1. 数据访问层实现
  • @author Jie.Yuan

*/
public class EmployeeDao implements IEmployeeDao {

@Override
public void getAll(PageBean pb) {
    
    
    int totalCount = this.getTotalCount();
    pb.setTotalCount(totalCount);
    
    
  
    if (pb.getCurrentPage() <=0) {
        pb.setCurrentPage(1);                        
    } else if (pb.getCurrentPage() > pb.getTotalPage()){
        pb.setCurrentPage(pb.getTotalPage());      
    }
    
    
    int currentPage = pb.getCurrentPage();
    int index = (currentPage -1 ) * pb.getPageCount();        
    int count = pb.getPageCount();                         
    
    
  
    String sql = "select * from employee limit ?,?";
    
    try {
        
        QueryRunner qr = JdbcUtils.getQueryRuner();
       
        List pageData = qr.query(sql, new BeanListHandler(Employee.class), index, count);
       
        pb.setPageData(pageData);
        
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    
}

@Override
public int getTotalCount() {
    String sql = "select count(*) from employee";
    try {
       
        QueryRunner qr = JdbcUtils.getQueryRuner();
       
        Long count = qr.query(sql, new ScalarHandler());
        return count.intValue();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

}

jsp页面:

      
          
              当前${requestScope.pageBean.currentPage }/${requestScope.pageBean.totalPage }页       
              
              首页
              上一页 
              下一页 
              末页
          
      
      
  
序号 员工编号 员工姓名
${vs.count } ${emp.empId } ${emp.empName }
数据为空

你可能感兴趣的:(servlet+jsp实现页面分页功能)