谁有关于分页的代码 java的 我现在是从mysql数据库用limit查数据 然后从页面上显示首页 上一页之类的

东西 要用js活是jsp实现啊 我是刚接触java还不太懂 希望有比较全的代码 容易懂的给我发一份 谢谢

    package util;  import java.util.ArrayList;  import java.util.List;  import java.util.Locale;  import javax.servlet.http.HttpServletRequest;  public class PageListData {      private List dataArray = null;      private int totalCount = 0;      private int pageSize = 0;      private int currentPage = 1;      private int totalPage=0;      private List dataList;      private String footer;      public PageListData(){}      public PageListData(int totalCount,int pageSize,int currentPage,List dataList){          setTotalCount(totalCount);          setPageSize(pageSize);          setCurrentPage(currentPage);          setDataList(dataList);          setFooter(getFooter());      }      public List getDataArray() {          return dataArray;      }      public void setDataArray(List dataArray) {          this.dataArray = dataArray;      }      public int getTotalCount() {          return totalCount;      }      public void setTotalCount(int totalCount) {          this.totalCount = totalCount;      }      public int getPageSize() {          return pageSize;      }      public void setPageSize(int pageSize) {          this.pageSize = pageSize;      }      public int getCurrentPage() {          return currentPage;      }      public void setCurrentPage(int currentPage) {          this.currentPage = currentPage;      }      public List getDataList() {          return dataList;      }      public void setDataList(List dataList) {          this.dataList = dataList;      }      public int getTotalPage() {          if (totalCount % pageSize == 0) {              return totalCount / pageSize;          } else {              return totalCount / pageSize + 1;          }      }      public void setTotalPage(int totalPage) {          this.totalPage = totalPage;      }      public void setFooter(String footer){          this.footer=footer;      }      public String getFooter() {          StringBuffer pageStr = new StringBuffer("");          pageStr.append("<p class='pages'>");          int totalPages = getTotalPage();          int prevPage = getCurrentPage() - 1;          int nextPage=getCurrentPage()+1;          pageStr.append("<span style="color:#049ed0;" mce_style="color:#049ed0;">第" + currentPage + "页/共"                  + totalPages + "页</span>  ");          if (currentPage > 1)              pageStr                      .append("<span><a style="cursor: pointer;text-decoration:underline;color:#049ed0;" mce_style="cursor: pointer;text-decoration:underline;color:#049ed0;"onclick='document.getElementById(/"pages/").value=1;document.getElementById(/"pages/").form.submit();'>首页</a></span>  ");          if (currentPage == 1)              pageStr                      .append("<span style="color:#049ed0;" mce_style="color:#049ed0;">首页</span>    ");          if (currentPage > 1)              pageStr                      .append("<span><a style="cursor: pointer;text-decoration:underline;color:#049ed0;" mce_style="cursor: pointer;text-decoration:underline;color:#049ed0;" onclick='document.getElementById(/"pages/").value="                              + prevPage                              + ";document.getElementById(/"pages/").form.submit();'>上一页</a></span>  ");          if (currentPage <= 1)              pageStr                      .append("<span style="color:#049ed0;" mce_style="color:#049ed0;">上一页</span>  ");          if (currentPage < totalPages)              pageStr                      .append("<span><a style="cursor: pointer;text-decoration:underline;color:#049ed0;" mce_style="cursor: pointer;text-decoration:underline;color:#049ed0;" onclick='document.getElementById(/"pages/").value="                              + nextPage                              + ";document.getElementById(/"pages/").form.submit();'>下一页</a></span>  ");          if (currentPage >= totalPages)              pageStr.append("<span style="color:#049ed0;" mce_style="color:#049ed0;">下一页</span>  ");          if (currentPage < totalPages)              pageStr                      .append("<span><a style="cursor: pointer;text-decoration:underline;color:#049ed0;" mce_style="cursor: pointer;text-decoration:underline;color:#049ed0;" onclick='document.getElementById(/"pages/").value="                              + totalPages                              + ";document.getElementById(/"pages/").form.submit();'>末页</a></span>  ");          if (currentPage == totalPages)              pageStr                      .append("<span style="color:#049ed0;" mce_style="color:#049ed0;"末页</span>  ");          pageStr                  .append("<span style="color:#049ed0;" mce_style="color:#049ed0;">跳转至第:<input type='text' value='"                          + currentPage                          + "'id='jumpPageBox' size='4' onblur='checkCurrentPage(document.getElementById(/"jumpPageBox/").value,"                          + totalPages                          + ")'/>页<input style="color:#049ed0;" mce_style="color:#049ed0;" type='button' value='跳转' onclick='document.getElementById(/"pages/").value=document.getElementById(/"jumpPageBox/").value;document.getElementById(/"pages/").form.submit();'/></span>");          pageStr.append("</p>");          pageStr.append("<input type='hidden' value='" + currentPage                  + "' name='currentPage' id='pages' />");          pageStr.append("<input type='hidden' value='" + pageSize                  + "' name='pageSize' id='pageSize' />");          return pageStr.toString();      }  }  
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-21
分页的精髓就是接收到当前页
当前页数据的起始位置=当前页*每页显示的数据-每页显示的数据
当前页数据终止位置=当前页*每页显示的数据追问

这我都知道啊 主要是我在jsp那就写不明白了 连接不好啊 我现在点击下一页的时候就出现NaN 就不行了 但是如果刚发布时候我先点击一下上一页的话在点击下一页就好使了 我也不知道怎么回事啊 有没有完整点的容易点的代码给我看看

追答

我把我的思路告诉你,首先你通过连接进入这个分页页面的时候后台接受的当前页是第一页,之后你根据我上面说出的算法把第一页的起始位置和终止位置算出来,之后在后台把当前页面保存到request.setAttribute中,之后在去查出你这些数据的总数,根据总数/每页显示的数,能得到最后一页,这时你需要判断能否整除,如果不能整除就得+1,因为有可能一共有20条数据,每页显示15条,这就会有2页,前台接收这个当前页,之后点击下一页按钮就是当前页加1、上一页按钮就是当前页-1、最后页这块很重要,你需把最后页的页接收到放到连接中发送给后台。

本回答被提问者和网友采纳
相似回答