struts2+hibernate(分页实现)

//Dao类中实现了list集合和pagetotal方法



package zjf.strhib.Dao;



import java.util.ArrayList;

import java.util.List;



import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;



public class PageDao {



 public List pageQuery(int pageSize, int pageNow) {



  List list = new ArrayList();

  SessionFactory sessionfactory = new Configuration().configure()

    .buildSessionFactory();

  Session session = sessionfactory.openSession();

  try {

   // session =new

   // Configuration().configure().buildSessionFactory().openSession();

   session.beginTransaction();



   // 得到每页显示的LevTwoInFuUser的对象的集合list

   list = session.createQuery("from user  order by id")

     .setFirstResult(pageNow * pageSize - pageSize)

     .setMaxResults(pageSize).list();

   session.getTransaction().commit();

  } catch (Exception e) {

   e.printStackTrace();

   session.getTransaction().rollback();

  } finally {

   // HibernateUtils.getSessionFactory().close();

   // NewHibernateUtil.closesessicon();

   session.close();

  }

  return list;

 }



 public int pageTotle(int pageSize, int pageNow) {



  int i = 0;

  SessionFactory sessionfactory = new Configuration().configure()

    .buildSessionFactory();

  Session session = sessionfactory.openSession();

  try {

   // session = HibernateUtils.getSessionFactory().getCurrentSession();

   session.beginTransaction();



   // i是查询结果条数

   i = session.createQuery("from user").list().size();



   // 用总的结果条数对每页显示的条数取余,得到总页数

   i = i % pageSize == 0 ? i / pageSize : i / pageSize + 1;



   session.getTransaction().commit();

  } catch (Exception e) {

   e.printStackTrace();

   session.getTransaction().rollback();

  } finally {

   // HibernateUtils.getSessionFactory().close();

   // NewHibernateUtil.closesessicon();

   session.close();

  }

  return i;

 }

}

//Pagequery中实现了Dao接口



package zjf.strhib.action;



import java.util.List;



import zjf.strhib.Dao.PageDao;



public class Pagequery {

 private List list;

    private int pageNow = 1 ; //初始化为1,默认从第一页开始显示

    private int pageSize = 2 ; //每页显示3条记录

    private int pageTotle= 1 ;//总页数

   

    private PageDao pageDao = new PageDao () ;

   

    public List getList() {

  return list;

 }

 public void setList(List list) {

  this.list = list;

 }

 public int getPageNow() {

  return pageNow;

 }

 public void setPageNow(int pageNow) {

  this.pageNow = pageNow;

 }

 public int getPageSize() {

  return pageSize;

 }

 public void setPageSize(int pageSize) {

  this.pageSize = pageSize;

 }

 public int getPageTotle() {

  return pageTotle;

 }

 public void setPageTotle(int pageTotle) {

  this.pageTotle = pageTotle;

 }

 

 public String fenye(){

   //得到每页显示的对象的集合list

        list  = pageDao.pageQuery(pageSize, pageNow);

        

        //得到总页数pageTotle

        pageTotle=pageDao.pageTotle(pageSize, pageNow);

        

        return "success";

 }

}

//Page页面显示



<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>



<%

    String path = request.getContextPath();

    String basePath = request.getScheme() + "://"

            + request.getServerName() + ":" + request.getServerPort()

            + path + "/";

%>

<%@taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

 <head>

  <SCRIPT type="text/javascript" src="js/calendar.js"></SCRIPT>

  <base href="<%=basePath%>">

  <title>分页</title>

  <meta http-equiv="pragma" content="no-cache">

  <meta http-equiv="cache-control" content="no-cache">

  <meta http-equiv="expires" content="0">

  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

  <meta http-equiv="description" content="This is my page">

 </head>

 <body>

  <table border="1">

   <tr>

    <th>

     用户编号

    </th>

    <th>

     真实姓名

    </th>



   </tr>



   <!-- 迭代查询 -->

   <s:iterator value="list">

    <tr>

     <td>

      <s:property value="name" />

     </td>

     <td>

      <s:property value="password" />

     </td>

    </tr>

   </s:iterator>

  </table>



  <!-- 首页 -->

  <s:url id="url_first" value="page.action">

   <s:param name="pageNow" value="1"></s:param>

  </s:url>

  <!-- 上一页 -->

  <s:url id="url_pre" value="page.action">

   <s:param name="pageNow" value="pageNow-1"></s:param>

  </s:url>

  <!-- 下一页 -->

  <s:url id="url_next" value="page.action">

   <s:param name="pageNow" value="pageNow+1"></s:param>

  </s:url>

  <!-- 末页 -->

  <s:url id="url_last" value="page.action">

   <s:param name="pageNow" value="pageTotle"></s:param>

  </s:url>



  <!-- 如果不是首页则提供首页的链接,如果是首页则不提供链接,以下类似 -->

  <s:if test="pageNow != 1">

            [<s:a href="%{url_first}">首页</s:a>]     

        </s:if>

  <s:else>

            [首页]

        </s:else>



  <s:if test="pageNow>1">

            [<s:a href="%{url_pre}">上一页</s:a>] 

        </s:if>

  <s:else>

            [上一页] 

        </s:else>

  <s:if test=" pageTotle > pageNow ">

            [<s:a href="%{url_next}">下一页</s:a>]

        </s:if>

  <s:else>

            [下一页]

        </s:else>



  <s:if test="pageTotle != pageNow">

            [<s:a href="%{url_last}">末页</s:a>]

        </s:if>

  <s:else>

            [末页]

        </s:else>

  第${pageNow}页/ 共${pageTotle}页

  

 </body>

</html>

 

你可能感兴趣的:(Hibernate)