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页面:
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实例的话,那么当前页、总页数等这些信息又放在哪呢?