hyblyw 2009-07-22 11:46
浏览 209
已采纳

分页问题

public class Page {
private String hql = "";
private int totalCount = 0; // 总记录数
private int pageCount = 1; // 每页显示记录数
private int totalPageNum = 1; // 总页数
private int currentPageNum = 1; // 当前页
private boolean hasPreviousPage = false; // 是否有上一页
private boolean hasNextPage = false; // 是否有下一页

public void init(String from, String where, String orderBy, int pageCount) {
    this.hql = from + " " + where + " " + orderBy;
    this.pageCount = pageCount;

    Session session = HibernateSessionFactory.getSession();
    this.totalCount = ((Long) session.createQuery(
            "select count(*)" + " " + this.hql).uniqueResult()).intValue(); // 计算总记录数
    session.close(); // ---------------------------------------------------------
    if (this.totalCount % this.pageCount == 0) { // 计算总页数
        this.totalPageNum = this.totalCount / this.pageCount;
        if (this.totalPageNum == 0) {
            this.totalPageNum = 1;
        }
    } else {
        this.totalPageNum = this.totalCount / this.pageCount + 1;
    }
    if (this.totalPageNum > 1) { // 判断是否有下一页
        this.hasNextPage = true;
    }

}

public void count() {
    Session session = HibernateSessionFactory.getSession();
    this.totalCount = ((Long) session.createQuery(
            "select count(*)" + " " + this.hql).uniqueResult()).intValue(); // 计算总记录数
    session.close();
}

public void pageNum() {
    if (this.totalCount % this.pageCount == 0) { // 计算总页数
        this.totalPageNum = this.totalCount / this.pageCount;
        if (this.totalPageNum == 0) {
            this.totalPageNum = 1;
        }
    } else {
        this.totalPageNum = this.totalCount / this.pageCount + 1;
    }
}

private List getPageMessage() {
    Query query = HibernateSessionFactory.getSession().createQuery(hql);
    query.setFirstResult((this.currentPageNum - 1) * this.pageCount);
    query.setMaxResults(this.pageCount);
    List list = query.list();
    return list;
}

public List getPreviousPage() {
    if (this.currentPageNum > 1) {
        this.currentPageNum = this.currentPageNum - 1;
        if (this.currentPageNum > 1) {
            this.hasPreviousPage = true;
        } else {
            this.hasPreviousPage = false;
        }
        this.hasNextPage = true;
    }
    return this.getPageMessage();
}

public List getCurrentPage() {
    return this.getPageMessage();
}

public List getNextPage() {
    if (this.currentPageNum < this.totalPageNum) {
        this.currentPageNum = this.currentPageNum + 1;
        this.hasPreviousPage = true;
        if (this.currentPageNum < this.totalPageNum) {
            this.hasNextPage = true;
        } else {
            this.hasNextPage = false;
        }
    }
    return this.getPageMessage();
}

public List getAppointPage(int appointPage) {
    if (appointPage > 0 & appointPage <= this.totalPageNum) {
        this.currentPageNum = appointPage;
        if (this.currentPageNum > 1) {
            this.hasPreviousPage = true;
        } else {
            this.hasPreviousPage = false;
        }
        if (this.currentPageNum < this.totalPageNum) {
            this.hasNextPage = true;
        } else {
            this.hasNextPage = false;
        }
    }
    return this.getPageMessage();
}

}

这是我写的一个分页类,不知道为什么每次刷新的时候总是会执行上一次执行的Action,比如:我现在点了“下一页”,它跳到第2页,当我再点“刷新”时,它就跳到第3页了。
[b]问题补充:[/b]
JSP页面:

function downCheck(){ document.forms[0].action="/Hzs/action/downPageAction.do"; }

Action代码:

ActionForward forward = mapping.findForward("downPage");
Collection members = null;
HttpSession session = request.getSession();
Page page = (Page) session.getAttribute("page");
String str = request.getParameter("appointPage");
if (str != "") {
forward = mapping.findForward("goPage");
} else {
members = page.getNextPage();
request.setAttribute("members", members);
}
return forward;

[b]问题补充:[/b]
请求:
http://127.0.0.1:8080/Hzs/action/downPageAction.do?appointPage=
[b]问题补充:[/b]
我知道了每次“刷新”都会把那个上一次的Action再执行一次,但是怎么解决这个问题我就不知道了呀,请大家给点想法。
[b]问题补充:[/b]
如果每次请求都新建一个Page实例的话,那么当前页、总页数等这些信息又放在哪呢?

  • 写回答

11条回答 默认 最新

  • pjuneye 2009-07-22 15:16
    关注

    还有种修改的方法就是不要nextPage这种写法,修改成根据页码来去就好了.刷新的时候页码是不变的.也就不会更新内容了. LZ可以考虑下.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64