如下Servlet代码,执行后limit的两个参数值都是0:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码,获取页面的值,向页面传值 request.setCharacterEncoding("utf-8"); String command = request.getParameter("command"); String description = request.getParameter("description"); String currentPage = request.getParameter("currentPage"); //创建分页对象 Page page = new Page(); Pattern pattern = Pattern.compile("[0-9]{1,9}"); if(currentPage == null || !pattern.matcher(currentPage).matches()){ page.setCurrentPage(1); }else{ page.setCurrentPage(Integer.valueOf(currentPage)); } System.out.println(page.getDbIndex()+" "+page.getDbNumber()+" "+page.getCurrentPage()); request.setAttribute("command", command); request.setAttribute("description", description); request.setAttribute("page", page); // 查询消息列表 List<Message> commandList = listService.queryMessageListByPage(command,description,page); request.setAttribute("messageList", commandList); request.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(request, response); }
上面我用控制台打印page对象的属性值,和log4j的日志显示如下:
Page对象的实体类如下:
public class Page { private int totalNumber; //总条数 private int totalPage; //总页数 private int currentPage; //当前页 private int pageNumber = 5; //每页显示条数 private int dbIndex; //从数据库中第几条开始取 private int dbNumber; //每次取多少条记录 public void count(){ //计算总页数 int totalPageTemp = this.totalNumber/this.pageNumber; int plus = (this.totalNumber%this.pageNumber)==0?0:1; totalPageTemp += plus; if(totalPageTemp<=0){ totalPageTemp = 1; } this.totalPage = totalPageTemp; //设置当前页 if(this.totalPage < this.currentPage){ this.currentPage = this.totalPage; } if(this.currentPage < 1){ this.currentPage = 1; } //设置数据库limit参数 this.dbIndex = (this.currentPage-1)*this.pageNumber; this.dbNumber = this.pageNumber; } public int getTotalNumber() { return totalNumber; } public void setTotalNumber(int totalNumber) { this.totalNumber = totalNumber; this.count(); } 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 getPageNumber() { return pageNumber; } public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; this.count(); } public int getDbIndex() { return dbIndex; } public void setDbIndex(int dbIndex) { this.dbIndex = dbIndex; } public int getDbNumber() { return dbNumber; } public void setDbNumber(int dbNumber) { this.dbNumber = dbNumber; } }
请大家帮忙看下哪里有问题,谢谢!
是Dao层的问题,刚刚自己发现了,原因是没有先调用count()方法,并把值传到page参数中去。