初学,我做个分页, 页数和现实数目 实现了,可是就是点击 下一页 数据没有变化,
 
下面是我的做法 请高手 帮忙谢谢大家。
在线等。。
分页类:
package com.wf.struts.comment;public class PublicPage {
private int totalRows;//总行数
private int pageSize=2;//每页显示数目
private int totalPages;//总页数
private int currentPage;//当前页数
private int startRow;//当前数据库中起始行

String url=null;

public PublicPage(int _totalRows){
totalRows=_totalRows;
totalPages=totalRows/pageSize;
int mod=totalRows%pageSize;
if(mod>0){
totalPages++;
currentPage=0;
startRow=0;
}
} public int getTotalRows() {
return totalRows;
} public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getTotalPages() {
return totalPages;
} public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
} public int getCurrentPage() {
return currentPage;
} public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} public int getStartRow() {
return startRow;
} public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void first(){
currentPage=1;
startRow=1;
}
public void privious(){
if(currentPage==1){
return;
}
currentPage--;
startRow=(currentPage-1)*pageSize;
}
public void next(){
if(currentPage<totalPages){
currentPage++;
}
startRow=(currentPage-1)*pageSize;
}
public void last(){
currentPage=totalPages;
startRow=(currentPage-1)*pageSize;
}
public void refresh(int _currentPage){
currentPage=_currentPage;
if(currentPage>totalPages){
last();
}
} public void setUrl(String url) {
this.url = url;
}
public boolean hasFirstPage(){
if(getCurrentPage()>1){
return true;
}else return false;
}
public boolean hasLastPage(){
if((getCurrentPage()<getTotalPages())&&(getTotalPages()!=1)){
 return true;
      }
    else return false;
}
  /**
     *   是否有下一页 
     *   @return 是否有下一页 
     */   
 public boolean hasNextPage() {
     if (getTotalRows()>pageSize) {
       // currentPage++;
     //   startRow = (currentPage - 1) * pageSize;
        
         return (this.getCurrentPage()<this.getTotalPages());
       }
       else   return false;
   } /**
 * 是否有上一页
 * @return
 */
public boolean hasPreviousPage(){
return (this.getCurrentPage()>1);
}

 public String getHTML(){
         StringBuffer html = new StringBuffer();     if (hasFirstPage()){
         html.append("<a href=\"")
                 .append(url)
                 .append("?pageNo=1\">首页")
                 .append("</a>");         }else
         {
         html.append("   ")
             .append("首页");
         }
         
     if (hasPreviousPage()){
         html.append("   <a href=\"")
         .append(url)
         .append("?pageNo=")
         .append(getCurrentPage()-1)
         .append("\">上页")
         .append("</a>");
         } else{
       html.append("   ")
           .append("上页");
     }
                  
     if (hasNextPage()){
         html.append("   <a href=\"")
         .append(url)
         .append("?pageNo=")
         .append(getCurrentPage()+1)
         .append("\">下页")
         .append("</a>");
         } else{
       html.append("   ")
           .append("下页");
       }     if (hasLastPage()){
         html.append("   <a href=\"")
         .append(url)
         .append("?pageNo=")
         .append(getTotalPages())
         .append("\">末页")
         .append("</a>   ");
         } else{
         html.append("   ")
           .append("末页");
       }
         
         html.append("【第").append(getCurrentPage()).append("页】");         
         html.append("【共").append(getTotalPages()).append("页】");         
         html.append("【共").append(getTotalRows()).append("条】"); 
         return html.toString();     
         }


}
查询所有实现类
public List NotefindAll(int pageSize,int currentPage) {
Session session=null;
List noteList=null;
try {
//session=HibernateUtils.getSession();
session=HibernateFilter.getSession();
session.beginTransaction();
Query q=session.createQuery("from Note");
q.setFirstResult(currentPage);
q.setMaxResults(pageSize);
noteList=q.list();
session.getTransaction().commit();
} catch (HibernateException e) {
System.out.print("not list error");
e.printStackTrace();
session.getTransaction().rollback();
}/*finally{
HibernateUtils.closeSession(session);
}*/

return noteList;
}action控制类 @Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

NoteForm notform=(NoteForm)form;

String PageNo=request.getParameter("PageNo");

if(null==PageNo||"".equals(PageNo.trim()))PageNo="1";
List findList=DaoFactory.getNoteDaoInstance().findAll();

int total=findList.size();

PublicPage pg=new PublicPage(total);
int pageNo=Integer.parseInt(PageNo);
String root = request.getContextPath();
pg.setUrl(root+"/notefindAll.do");
pg.setCurrentPage(pageNo);

int pageSize=pg.getPageSize();
int cpgae=pg.getCurrentPage();
int begin=(cpgae-1)*pageSize;

List rgPage=DaoFactory.getNoteDaoInstance().NotefindAll(pageSize,begin ); request.setAttribute("showPage",pg.getHTML());
request.setAttribute("notelist",rgPage);

return mapping.findForward("list");
}
}我感觉是我参数传的问题 可是又不知道如何更改,请高手 各位师哥师姐 帮忙 小弟感激不尽。

解决方案 »

  1.   

    我在 JSP页面  C:out 拿到 actioN 中的showPage 显示 上页下页 首页 
      

  2.   

    public void privious(){ 
    if(currentPage==1){ 
    return; 

    currentPage--; 
    startRow=(currentPage-1)*pageSize; 

    这里的currentPage--是什么意思?
      

  3.   

    PublicPage pg=new PublicPage(total); 
    new PublicPage(total); 的时候你好象把当前页赋值为0了吧.
    这样你获取的begin是不是就有错误了  你跟踪一下代码看看每次点下一页的时候
    int begin=(cpgae-1)*pageSize; begin的值是否是你期望的结果,先定位问题的原因.
      

  4.   

    我还是没看懂你这个部分,currentPage是当前页,currentPage-- 是上一页,你先把currentPage的值减一,然后又根据减一后的currentPage的值再减一算出本页的起始记录数??
      

  5.   


    currentPage ---当前页