1.创建一个分页组建PageModel.java

package com.zsw.util;

import java.util.List;
/**
* 分页组建
* @author zsw
* 时间:2009年2月13日11:11:35
*/
public class PageModel {

private int pageNo; //当前页数

private int pageSize=15; //每页显示多少条数据

private int totalRecords; //总记录

private List list; //获取结果集

/**
* 获取总记录数
* @return
*/
public int getTotalPages(){
return (totalRecords+pageSize-1)/pageSize;
}

/**
* 获取首页
* @return
*/
public int getTopPage(){
return 1;
}

/**
* 获取上一页
* @return
*/
public int getUpPage(){
if(pageNo<=1){
return 1;
}
return pageNo-1;
}

/**
* 获取下一页
* @return
*/
public int getNextPage(){
if(pageNo>getTotalPages()){
return getTotalPages()==0?1:getTotalPages();
}
return pageNo+1;
}

/**
* 获取最后一页
* @return
*/
public int getBottomPage(){
return getTotalPages()==0?1:getTotalPages();
}







public int getPageNo() {
return pageNo;
}

public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getTotalRecords() {
return totalRecords;
}

public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}

public List getList() {
return list;
}

public void setList(List list) {
this.list = list;
}



}

2.用户dao层

package com.zsw.dao.impl;

import java.util.List;

import org.hibernate.Session;

import com.zsw.dao.UserDAO;
import com.zsw.entity.HibernateSessionFactory;
import com.zsw.util.PageModel;

public class UserDAOImpl implements UserDAO {

/**
* 获取所有的集合
*/
public PageModel getAll(int pageNo, int pageSize) {

Session session = HibernateSessionFactory.getSession();
session.beginTransaction();

List list = session.createQuery("from TUser")
.setFirstResult((pageNo-1)*pageSize)
.setMaxResults(pageSize)
.list();

int count = (Integer)session.createQuery("select count(*) from TUser").uniqueResult(); //获取总记录数


System.out.println(pageNo);
PageModel pageModel = new PageModel();
pageModel.setList(list);
pageModel.setPageNo(pageNo);
pageModel.setPageSize(pageSize);
pageModel.setTotalRecords(count);

session.getTransaction().commit();

return pageModel;
}

}

3.页面

<html:html lang="true">
<head>
<title>userList.jsp</title>
<mce:script type="text/javascript"><!--
function getTopPage(){
window.location.href="user.do?op=getList&pageSize={userForm.pageSize}&pageNo=${pagemodel.topPage}";
}
function getNextList(){
window.location.href="user.do?op=getList&pageSize={userForm.pageSize}&pageNo=${pagemodel.nextPage}";
}
function getUpPage(){
window.location.href="user.do?op=getList&pageSize={userForm.pageSize}&pageNo=${pagemodel.upPage}";
}
function getBottomPage(){
window.location.href="user.do?op=getList&pageSize={userForm.pageSize}&pageNo=${pagemodel.bottomPage}";
}

// --></mce:script>
</head>
<body>
<logic:iterate id="user" name="list">
${user.userName }<br>
</logic:iterate>
<br>
<div style="border:1px solid red;" mce_style="border:1px solid red;" align="center" >
<input type="button" value="首页" οnclick="getTopPage()">
<input type="button" value="上一页" οnclick="getUpPage()">
<input type="button" value="下一页" οnclick="getNextList()">
<input type="button" value="最后一页" οnclick="getBottomPage()">
</div>
总记录数:${pagemodel.totalRecords } |
总页数:${pagemodel.totalPages } |
当前页数:${pagemodel.pageNo } |
每页显示页数:${pagemodel.pageSize }
</body>
</html:html>