//分页类
public class Pager {
private int pageNow = 1;//
private int pageSize = 7;//
private int totalPage;//
private int totalSize;//
public Pager(int pageNow, int totalSize) {
this.pageNow = pageNow;
this.totalSize = totalSize;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
totalPage = getTotalSize() / getPageSize();
if (totalSize % pageSize != 0)
totalPage++;
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public boolean isHasFirst() {
if (pageNow == 1)
return false;
else
return true;
}
public void setHasFirst(boolean hasFirst) {
}
public boolean isHasPre() {
if (this.isHasFirst())
return true;
else
return false;
}
public void setHasPre(boolean hasPre) {
}
public boolean isHasNext() {
if (isHasLast())
return true;
else
return false;
}
public void setHasNext(boolean hasNext) {
}
public boolean isHasLast() {
if (pageNow == this.getTotalPage())
return false;
else
return true;
}
public void setHasLast(boolean hasLast) {
}
}
//service层
public class PageService {
@SuppressWarnings("unchecked")
public List<?> list(int pageNow, int pageSize, String hql) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
List<Object> objects;
Query query = session.createQuery(hql);
query.setFirstResult(pageSize * (pageNow - 1));
query.setMaxResults(pageSize);
objects = query.list();
tx.commit();
return objects;
}
}
//在action中调用public String listUser() {
String hql = "from Userinfo u";
if (ps.list(pageNow, pageSize, hql) != null) {
userinfos = (List<Userinfo>) ps.list(pageNow, pageSize, hql);
Map<String, Object> request = (Map<String, Object>) ActionContext
.getContext().get("request");
Pager page = new Pager(this.getPageNow(), us.getUserSize());
request.put("userinfos", userinfos);
request.put("page", page);
return Action.SUCCESS;
} else {
return Action.LOGIN;
}
}
//jsp中
<body>
<table width="832" border="0" cellpadding="0" cellspacing="0" id="listBook">
<tr bgcolor="#E7E7E9">
<td width="5%" height="40"> </td>
<td width="25%" colspan="2" bgcolor="#E7E7E9"><div align="center" class="STYLE10">邮箱</div></td>
<td width="25%" colspan="2" bgcolor="#E7E7E9" class="STYLE1"><div align="center" class="STYLE10">密码</div></td>
<td width="25%" colspan="2" bgcolor="#E7E7E9" class="STYLE1"><div align="center" class="STYLE10">权限</div></td>
<td width="8%" bgcolor="#E7E7E9"><span class="STYLE8"></span></td>
<td width="8%" bgcolor="#E7E7E9"><span class="STYLE8"></span></td>
</tr>
<s:iterator value="#request.userinfos" id="oneUser">
<tr>
<td height="50">
<div align="center">
<input type="checkbox" name="checkbox" value="checkbox" />
</div></td>
<td width="5%"></td>
<td width="23%" class="STYLE4"><s:property
value="#oneUser.email" /></td>
<td width="5%" class="STYLE4"></td>
<td width="23%"><span class="STYLE4"><s:property
value="#oneUser.password" /></span></td>
<td width="5%" class="STYLE4"></td>
<td width="23%"><span class="STYLE4">
<s:if test="#oneUser.power==1">
普通用户
</s:if>
<s:else>
管理员
</s:else>
</span></td>
<td><div align="right" class="STYLE1"><a href='deleteUser?userid=<s:property value="#oneUser.id"/>' class="STYLE5">删除|</a></div></td>
<td class="STYLE1"><a href='lookUser?userid=<s:property value="#oneUser.id"/>&pageNow=<s:property value="#request.page.pageNow"/>' target="_self" class="STYLE5">修改</a></td>
</tr>
</s:iterator>
<tr>
<td colspan="9"><table width="832" border="0" cellspacing="0" bgcolor="#E7E7E9">
<s:set name="page" value="#request.page"></s:set>
<tr>
<td width="70%"> </td>
<s:if test="#page.isHasPre()">
<td width="10%"><a href='listUser?pageNow=<s:property value="#page.pageNow-1"/>' target="_self" class="STYLE3" >上一页</a></td>
</s:if>
<s:else>
<td width="10%"><a href="listUser?pageNow=1" target="_self" class="STYLE3" >上一页</a></td>
</s:else>
<s:if test="#page.isHasNext()">
<td width="10%"><a href="listUser?pageNow=<s:property value="#page.pageNow+1"/>" target="_self" class="STYLE3">下一页</a></td>
</s:if>
<s:else>
<td width="10%"><a href="listUser?pageNow=<s:property value="#page.totalPage"/>" target="_self" class="STYLE3">下一页</a></td>
</s:else>
<td width="10%"><a href="listUser?pageNow=<s:property value="#page.totalPage"/>" target="_self" class="STYLE3">尾页</a></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
这是采用struts2+hibernate 做的,你可以参考一下