前台代码:
<a href="${ pageContext.request.contextPath }/yourAction_findByPage.action?page=1">分页查询</a>
工具类
//用来存储分页查询返回的记录集以及页数信息
public class PageBean<T> {
private int page;//当前页数
private int totalCount;//总记录书
private int totalPage;//总页数
private int limit;//每页显示的记录数
private List<T> list;//每页显示的数据集合
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
action代码
//接收页数
private Integer page;
public void setPage(Integer page) {
this.page = page;
}
public String findByPage(){
PageBean<T>pageBean=yourService.findByPage(page);
ActionContext.getContext().getValueStack().set("pageBean", pageBean);
return "findByPage";
}
逻辑层代码
public PageBean<T> findByPage(Integer page) {
PageBean<T> pageBean = new PageBean<T>();
//设置当前页数
pageBean.setPage(page);
//设置每页显示的条数
int limit=10;
pageBean.setLimit(limit);
//查询总的记录数
int totalCount=orderDao.findCount();
int totalPage = 0;
//设置总页数
if (totalCount% limit == 0) {
totalPage=totalCount / limit ;
}else {
totalPage = totalCount/limit +1;
}
pageBean.setTotalCount(totalCount);
pageBean.setTotalPage(totalPage);
//定义开始的条数
int begin =0;
begin = (page-1)*limit;
List<T>list=orderDao.findByPage(begin,limit);
pageBean.setList(list);
return pageBean;
}
持久层代码
public int findCount() {
String hql = "select count(*) from T";
org.hibernate.Query query = this.getSession().createQuery(hql);
int count = ((Long) query.iterate().next()).intValue();
return count;
}
public List<T> findByPage(int begin, int limit) {
String hql = "from T order by datetime desc";
List<T> list=this.getHibernateTemplate().execute(new PageHibernateCallback<T>(hql, null, begin, limit));
if (list != null && list.size()>0) {
return list;
}
return null;
}
spring配置
<bean id="yourAction" class="myTest.YourAction" scope="prototype">
<property name="yourService" ref="yourService"></property>
</bean>
struts配置
<action name="your_*" class="yourAction" method="{1}">
<result name="findByPage">/webInfo/list.jsp</result>
</action>
list.jsp代码
<tr align="center">
<td colspan="7">
第<s:property value="pageBean.page"/>/<s:property value="pageBean.totalPage"/>页
<s:if test="pageBean.page != 1">
<a href="${ pageContext.request.contextPath }/your_findByPage.action?page=1">首页</a>|
<a href="${ pageContext.request.contextPath }/your_findByPagel.action?page=<s:property value="pageBean.page-1"/>">上一页</a>|
</s:if>
<s:if test="pageBean.page != pageBean.totalPage">
<a href="${ pageContext.request.contextPath }/your_findByPage.action?page=<s:property value="pageBean.page+1"/>">下一页</a>|
<a href="${ pageContext.request.contextPath }/your_findByPage.action?page=<s:property value="pageBean.totalPage"/>">尾页</a>
</s:if>
</td>