hibernate+struts2+jsp分页
Hibernate分页查询代码
/** * 分页查询 */ public List<Ptuser> fenye(int pagenum,int num_one) { //获得session Session session = hibernateTemplate.getSessionFactory().openSession(); //开启事务 session.beginTransaction(); String str = "from ptuser"; //创建查询对象 Query query = session.createQuery(str); //设置查询开始值 query.setFirstResult((pagenum-1)*num_one); //设置查询最大获取数量 query.setMaxResults(num_one); List<Ptuser> ptusers = query.list(); session.getTransaction().commit(); //关闭session session.close(); return ptusers; }
action 代码
public class PtuserAction extends ActionSupport implements ModelDriven{ /** * */ private static final long serialVersionUID = -8019056962412775442L; private PtuserManager ptuserManager; private PtuserInfo info = new PtuserInfo(); private List<Ptuser> ptusers; private Ptuser ptuser; //总页数 private int num_allblogpage; //总列数 private int num_allblog; //页码 private int pagenum; //一页的列数 private int num_one; //分页标码的开始值 private int page_begin = 1; //每页显示的页码个数 private int num_feye; /** * 分页查询 */ public String fenye(){ //获得普通用户的所有列 List<Ptuser> list = ptuserManager.getPtusers(); //得到总列数 num_allblog = list.size(); //用于测试,先指定每页显示条数 num_one = 5; //得到总页数 num_allblogpage = (num_allblog%num_one==0?(num_allblog/num_one):(num_allblog/num_one)+1); //如果页码大于总页数,页码就设为等于总页数 if(pagenum>num_allblogpage){ pagenum = num_allblogpage; }else if(pagenum<1){ //如果页码数小于1,页码就设为等于1 pagenum = 1; } //页码显示 每页显示5条 if(pagenum%5==0){ //如果页码数为5的倍数,则设页码开始数为当前页码数 page_begin = pagenum; }else if(pagenum/5>0){ //如果页码数除5的值大于0,则设页码开始数等于于数乘5 page_begin = (pagenum/5)*5; }else{ page_begin = 1; } //设置分页码的范围值 num_feye = page_begin+4; if(num_feye>num_allblogpage){ num_feye = num_allblogpage; } //获取指定页的列,根据页码和显示条数,得到分页内容 ptusers = ptuserManager.fenye(pagenum, num_one); return "list"; }
JSP代码
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <tr> <td colspan="100"> <span> ${num_allblog}条数据 共${num_allblogpage}页 </span> <a href="ptuser!fenye.action?pagenum=${pagenum-1}">上一页</a> <c:forEach begin="${page_begin}" end="${num_feye}" var="pagenum"> [<a href="ptuser!fenye.action?pagenum=${pagenum}">${pagenum}</a>] </c:forEach> <a href="ptuser!fenye.action?pagenum=${pagenum+1}"> 下一页 </a> <a href="ptuser!fenye.action?pagenum=${num_allblogpage}"> 尾页</a> </td> </tr>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。