action开发
public String getEmployaction(){
int currentPage=pageBean.getCurrentPage();
System.out.println("当前页为"+currentPage);
List<TEmployee> emp=employeeBiz.getEmployeeByPage(currentPage);
for (int i = 0; i < emp.size(); i++) { //简单的调试,看是否能够从数据库区的数据,可以测试service和dao层是否正确
System.out.println("当前页员工为"+emp.get(i));
}
System.out.println("当前页员工数"+emp.size());
pageBean=employeeBiz.getPage();//调用业务层的方法,封装分页实体的属性
int totalPage=pageBean.getTotalPage();//从封装的分页实体获得页面总数
System.out.println("action的页数为"+totalPage);//调试代码
pageBean.setCurrentPage(currentPage);//向前端传递当前页码
pageBean.setTotalPage(totalPage);//向前端传递页面总数
request.put("emp",emp);//向前端输出数据
return "success";
}
Biz层开发
@Service
public class EmployeeBizImp implements EmployeeBiz {
@Resource
private EmpDao empDao;
/**
* @param 当前的页码
* @return 员工对象
**/
@Override
public List<TEmployee> getEmployeeByPage(int currentPage) {
return empDao.getAllEmployee(currentPage);
}
/**
*@return 分页的实体对象
**/
@Override
public PageBean getPage(){
int count=empDao.getTotalCount();//调用dao层的获取总记录数的方法
PageBean pageBean=new PageBean();//实例化一个PageBean对象
pageBean.setTotalCount(count);//设置总记录数,为在PageBean中计算页数
pageBean.setLimitCount(3);//设置分页的大小,计算页面总数
System.out.println("总记录数为"+count);//
return pageBean;
}
}
Dao层
@Resource
private HibernateTemplate hibernatetemplate;
@SuppressWarnings("unchecked")
/**
*分页查询员工数数据
*@param 当前传入的页码
**/
@Override
public List<TEmployee> getAllEmployee(int currentPage) {
String hql="select * from t_employee";
int pageSize =3;
int startCount=(currentPage-1)*pageSize;
return super.listPageBySQL(hql,startCount,pageSize);
}
@Override
public int getTotalCount() {
String sql="select count(*) from t_employee";
return super.computeRecordCountBySQL(sql);
}
实体
private static final long serialVersionUID = 1L;
private int currentPage;//当前页数
private int totalCount;//总记录数
private int totalPage;//总页数
private int limitCount;//每一页限制的记录数
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return (totalCount+limitCount-1)/limitCount;//(总的页数=总的记录数+页面大小)/页面大小
}
public void setTotalPage(int totalPage){
this.totalPage=totalCount;
}
public int getLimitCount() {
return limitCount;
}
public void setLimitCount(int limitCount) {
this.limitCount = limitCount;
}
Jsp页面
<!-- 下方分页栏 -->
<div align="center">
<div><br>第 ${pageBean.currentPage}/${pageBean.totalPage}页 </div>
<span><s:if test="pageBean.currentPage!=1">
<a href="${pageContext.request.contextPath }/employee/getEmployaction?pageBean.currentPage=1">首页</a>
<a href="${pageContext.request.contextPath }/employee/getEmployaction?pageBean.currentPage=${pageBean.currentPage-1}">上一页</a>
</s:if>
<s:if test="pageBean.currentPage!=pageBean.totalPage">
<a href="${pageContext.request.contextPath }/employee/getEmployaction?pageBean.currentPage=${pageBean.currentPage+1}">下一页</a>
<a href="${pageContext.request.contextPath }/employee/getEmployaction?pageBean.currentPage=${pageBean.totalPage}">尾页</a>
</s:if></span>
</div>