分页工具类
package com.EIMay.utils;
import java.util.List;
public class PageBean {
//当前页数
private Integer currentPage;
//总记录数
private Integer totalCount;
//每页显示条数
private Integer pageSize;
//总页数
private Integer totalPage;
//分页列表数据
private List list;
//起止索引
public Integer getStart(){
return (this.currentPage-1)*this.pageSize;
}
public PageBean(Integer currentPage, Integer totalCount, Integer pageSize) {
this.currentPage = currentPage;
this.totalCount = totalCount;
this.pageSize = pageSize;
//判断传值是否为空(第一次点击列表)
if(this.currentPage==null){
//默认等于第一页
this.currentPage = 1;
}
//判断传值是否为空(第一次点击列表)
if(this.pageSize==null){
//默认每页显示三条记录
this.pageSize =3;
}
//计算总页数
this.totalPage = (this.totalCount+this.pageSize-1)/this.pageSize;
//判断传入的页面数是否超出范围
if(this.currentPage < 1){
////不能小于1,默认等于第一页
this.currentPage = 1;
}
if(this.currentPage > this.totalPage){
//不能大于总页数,默认等于第一页
this.currentPage = this.totalPage;
}
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
action
public String list() throws Exception {
//1.使用离线查询的方式获取查询条件
DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
//判断是否为空并封装参数
if(StringUtils.isNotBlank(customer.getCust_name())){
//不为空则添加查询条件
dc.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%"));
}
//2.调用service方法封装返回记录
PageBean pb = cs.getPageBean(dc,currentPage,pageSize);
//3.将返回的pageBean存到request域
ActionContext.getContext().put("pageBean", pb);
return "list";
}
service
public PageBean getPageBean(DetachedCriteria dc, Integer currentPage,
Integer pageSize) {
//1.调用dao方法获得查询条件的总记录数
Integer totalCount = cd.getTotalCount(dc);
//2.构造PageBean,封装查询到的参数和传入的参数
PageBean pb = new PageBean(currentPage,totalCount,pageSize);
//3.调用dao方法获得查询列表
List<Customer> list = cd.getPageList(dc,pb.getStart(),pb.getPageSize());
//4将列表记录封装到PageBean
pb.setList(list);
return pb;
}
dao
@Override
public Integer getTotalCount(DetachedCriteria dc) {
//设置查询的聚合函数,总记录数
dc.setProjection(Projections.rowCount());
List<Long> list = (List<Long>) getHibernateTemplate().findByCriteria(dc);
//清空之前设置的聚合函数
dc.setProjection(null);
if(list!=null && list.size()>0){
Long count = list.get(0);
return count.intValue();
}else{
return null;
}
}
@Override
public List<T> getPageList(DetachedCriteria dc, Integer start, Integer pageSize) {
List<T> list = (List<T>) getHibernateTemplate().findByCriteria(dc, start, pageSize);
return list;
}
jsp
<s:iterator value="#pageBean.list" var="cust" >
<TR
style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
<TD>
<s:property value="#cust.cust_name" />
</TD>
<TD>
<s:property value="#cust.cust_level" />
</TD>
<TD>
<s:property value="#cust.cust_source" />
</TD>
<TD>
<s:property value="#cust.cust_linkman" />
</TD>
<TD>
<s:property value="#cust.cust_phone" />
</TD>
<TD>
<s:property value="#cust.cust_mobile" />
</TD>
<TD>
<s:if test="#parameters.select==null">
<a href="${pageContext.request.contextPath }/CustomerAction_toEdit?cust_id=<s:property value="#cust.cust_id"/>">修改</a>
<a href="${pageContext.request.contextPath }/customerServlet?method=delete&custId=${customer.cust_id}">删除</a>
</s:if>