分页原理及出现的错误

1 设置跳转
  
              ">上一页
              

              
             ">下一页
              

2 在Form中设置private String currentPage ;
3进入action,在action中,页面信息都是在pagebean得到的,根据页面信息,进行分页操作
public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  ListForm listForm = (ListForm) form;// TODO Auto-generated method stub
    HttpSession session = request.getSession();
      PageBean pb ;
      NewsDao newsdao = new NewsDao();
      List list = new ArrayList();
      String currentPage = listForm.getCurrentPage();
      if(currentPage == null){
      int totalRows = newsdao.getTotalRows();
      System.out.println(totalRows);
      pb = new PageBean(totalRows,1);
      session.removeAttribute("page");
      System.out.println("当前页面"+String.valueOf(pb.getPageStartRow()));
      list = newsdao.getPageQueryDate(String.valueOf(pb.getPageStartRow()),String.valueOf(pb.getPageRecorders()));
      System.out.println("进入action11");
      }
    
      else{
      
      
   int totalRows = newsdao.getTotalRows();
      pb = new PageBean(totalRows,Integer.parseInt(currentPage));
      System.out.println("当前页面"+String.valueOf(pb.getPageStartRow()));
      list = newsdao.getPageQueryDate(String.valueOf(pb.getPageStartRow()), String.valueOf(pb.getPageRecorders()));
      
      session.setAttribute("page",pb);      
      request.setAttribute("all", list);
      return mapping.findForward("pageinfo");
}
}
在action中,会涉及到三个方法
(1)
查询到数据库中记录的跳数
  public int getTotalRows() {
    int totalRows = 0;
    String sql = "select count(*) from Newsvo";
    Connection conn = this.session.connection();
    PreparedStatement pstmt;
  try {
   pstmt = conn.prepareStatement(sql);
   ResultSet rs =pstmt.executeQuery();
     if(rs.next()){
      
      String id = (String) rs.getString(1);
      totalRows = Integer.parseInt(id);
          
     }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    return totalRows;
  }
}
(2)
根据记录的条数,得到分页信息
public class PageBean {
private int currentPage = 1; // 当前页
private int totalPages = 0; // 总页数
private int pageRecorders = 5;// 每页5条数据
private int totalRows = 0; // 总数据数
private int pageStartRow = 0;// 每页的起始数
private int pageEndRow = 0; // 每页显示数据的终止数
private boolean hasNextPage = false; // 是否有下一页
private boolean hasPreviousPage = false; // 是否有前一页
private int nextPage = 0;//下一页的页码
private int previousPage = 0;//上一页的页码
  public PageBean(int totalRows,int currentPage) {
    this.totalRows = totalRows;
    this.currentPage = currentPage;
    if(currentPage < 2)
     hasPreviousPage = false;
    else
     hasPreviousPage = true;
    if ((totalRows % pageRecorders) == 0) {
     totalPages = totalRows / pageRecorders;
    } else {
     totalPages = totalRows / pageRecorders + 1;
    }
    if (currentPage < totalPages) {
     hasNextPage = true;
     nextPage = currentPage + 1;
     pageStartRow = (currentPage - 1)*pageRecorders;
     this.pageEndRow = pageStartRow + pageRecorders;
    } else if(currentPage == totalPages){
     pageStartRow = (currentPage - 1)*pageRecorders;
     this.pageEndRow = totalRows;
     hasNextPage = false;
     nextPage = currentPage;
    }
    if(currentPage < 2){
     previousPage = currentPage;
     hasPreviousPage = false;
    }else if(currentPage > 1){
     previousPage = currentPage-1;
     hasPreviousPage = true;
    }
   }
public int getCurrentPage() {
  return currentPage;
}
public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
}
public boolean isHasNextPage() {
  return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
  this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
  return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
  this.hasPreviousPage = hasPreviousPage;
}
public int getNextPage() {
  return nextPage;
}
public void setNextPage(int nextPage) {
  this.nextPage = nextPage;
}
public int getPageEndRow() {
  return pageEndRow;
}
public void setPageEndRow(int pageEndRow) {
  this.pageEndRow = pageEndRow;
}
public int getPageRecorders() {
  return pageRecorders;
}
public void setPageRecorders(int pageRecorders) {
  this.pageRecorders = pageRecorders;
}
public int getPageStartRow() {
  return pageStartRow;
}
public void setPageStartRow(int pageStartRow) {
  this.pageStartRow = pageStartRow;
}
public int getPreviousPage() {
  return previousPage;
}
public void setPreviousPage(int previousPage) {
  this.previousPage = previousPage;
}
public int getTotalPages() {
  return totalPages;
}
public void setTotalPages(int totalPages) {
  this.totalPages = totalPages;
}
public int getTotalRows() {
  return totalRows;
}
public void setTotalRows(int totalRows) {
  this.totalRows = totalRows;
}
}
(3)根据分页信息,把数据查询出来

public List getPageQueryDate(String startRow,String num){
  List list = new ArrayList();
  System.out.println("进入查询");

  String sql="select * from Newsvo limit "+startRow+","+num;
  Connection conn = this.session.connection();
  PreparedStatement pstmt ;
  try {
   pstmt = conn.prepareStatement(sql);
   ResultSet rs = pstmt.executeQuery();
  
   while(rs.next()){
  
    Newsvo news = new Newsvo();
    news.setId(rs.getInt("id"));
    news.setUrl(rs.getString("url"));
    //news.setUrl(newsvo.getUrl());
    news.setTitle(rs.getString("title"));
    System.out.println(rs.getString("title"));
    list.add(news);
   } } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
  
  return list;
}

错误:dao中,
while(rs.next()){
    Newsvo news = new Newsvo();
    news.setId(rs.getInt("id"));
    news.setUrl(rs.getString("url"));
    //news.setUrl(newsvo.getUrl());
    news.setTitle(rs.getString("title"));
    System.out.println(rs.getString("title"));
    list.add(news); }
我把vo的定义,放在while以外,发生了查询的是同一条数据。

你可能感兴趣的:(struts)