PageModel
package com.xy.util;
import Java.util.List;
public class PageModel { private List datas; // 数据集合 private int total; // 数据总数 private int totalPage; // 数据可分的页数
public PageModel() { super(); }
public PageModel(List datas, int total, int totalPage) { super(); this.datas = datas; this.total = total; this.totalPage = totalPage; }
public List getDatas() { return datas; }
public int getTotal() { return total; }
public int getTotalPage() { return totalPage; }
public void setDatas(List datas) { this.datas = datas; }
public void setTotal(int total) { this.total = total; }
public void setTotalPage(int totalPage) { this.totalPage = totalPage; } }
IStudentDao
package com.xy.dao;
import com.xy.pojo.Student; import com.xy.util.PageModel;
public interface IStudentDao { // 分页 public PageModel GetAllStudent(int start, int pagesize);
}
StuentDaoImpl
package com.xy.dao.impl;
import java.util.List; import com.xy.dao.IStudentDao; import com.xy.pojo.Student; import com.xy.util.PageModel;
public class StuentDaoImpl extends BaseDaoHibernateImpl implements IStudentDao {
public PageModel GetAllStudent(int start, int pagesize) { String sqlCount = "select count(*) from Student"; int count = ((Long) this.getSession().createQuery(sqlCount).uniqueResult()).intValue(); List lstStu = this.getSession().
createQuery("from Student s join fetch s.classes").setFirstResult(start).setMaxResults
(pagesize).list(); int totalPage = 0; if (count % pagesize == 0) { totalPage = (count / pagesize); } else { totalPage = (count / pagesize) + 1; }
// 得到结果集 PageModel pm = new PageModel(); pm.setTotal(count); pm.setDatas(lstStu); pm.setTotalPage(totalPage); return pm; } }
ToMainAction package com.xy.action;
import com.opensymphony.xwork2.ActionSupport; import com.xy.dao.IStudentDao; import com.xy.util.PageModel;
public class ToMainAction extends ActionSupport { private int pageIntNum; // 用来计算的页码 private String pageNum; // 传来的字符串参数 private IStudentDao sdao; private PageModel stuList; private int totalPage;
public String execute() { if (null != pageNum && !"".equals(pageNum)) { pageIntNum = Integer.valueOf(pageNum); int start = (pageIntNum - 1) * 2; stuList = sdao.GetAllStudent(start, 2);
} else { pageIntNum = 1; stuList = sdao.GetAllStudent(0, 2); } return SUCCESS; }
public int getPageIntNum() { return pageIntNum; }
public String getPageNum() { return pageNum; }
public IStudentDao getSdao() { return sdao; }
public PageModel getStuList() { return stuList; }
public int getTotalPage() { return totalPage; }
public void setPageIntNum(int pageIntNum) { this.pageIntNum = pageIntNum; }
public void setPageNum(String pageNum) { this.pageNum = pageNum; }
public void setSdao(IStudentDao sdao) { this.sdao = sdao; }
public void setStuList(PageModel stuList) { this.stuList = stuList; }
public void setTotalPage(int totalPage) { this.totalPage = totalPage; } }
Main.jsp
<s:iterator value="stuList.datas" var="stu" status="index"> <tr οnmοuseοver="this.style.backgroundColor='#DBE4FF';this.style.cursor='hand'" οnmοuseοut="this.style.backgroundColor='#ffffff';this.style.cursor='default'" οndblclick="modify(<s:property value="#stu.stuId"/>)"> <td> <s:checkbox name="stuids" fieldValue="%{#stu.stuId}" /> </td> <td> <s:property value="#stu.stuId" /> </td> <td> <s:property value="#stu.stuName" /> </td> <td> <s:if test="#stu.stuSex==1">男</s:if> <s:else>女</s:else> </td> <td> <s:property value="#stu.stuAge" /> </td> <td> <s:date name="#stu.stuBirth" format="yyyy年MM月dd日" /> </td> <td> <s:property value="#stu.classes.className" /> </td> <td> <s:if test="#stu.stuStatus==1">在职</s:if> <s:else>离职</s:else> </td> <td> <a href='stu_todel?id=<s:property value="#stu.stuId" />'>删除</a> </td> </tr> </s:iterator> </table> </s:form>
<s:if test="data.totalPage>1"> <s:if test="stuList.totalPage==pageIntNum">
<a href="ToMainAction?pageNum=1">首页</a> <a href="ToMainAction?pageNum=<s:property value="pageIntNum-1"/>">上一页</a> </s:if> <s:elseif test="pageIntNum==1"> <a href="ToMainAction?pageNum=<s:property value="pageIntNum+1"/>">下一页</a>
<a href="ToMainAction?pageNum=<s:property value="data.totalPage"/>">末页</a> </s:elseif> <s:else>
<a href="ToMainAction?pageNum=1">首页</a> <a href="ToMainAction?pageNum=<s:property value="pageIntNum-1"/>">上一页</a> <a href="ToMainAction?pageNum=<s:property value="pageIntNum+1"/>">下一页</a>
<a href="ToMainAction?pageNum=<s:property value="data.totalPage"/>">末页</a>
</s:else> 当前第<s:property value="pageIntNum" />页 共<s:property value="data.totalPage" />页
</s:if>
SSH的配置与一般项目无区别。