欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用hibernate和struts2实现分页功能的示例

程序员文章站 2024-03-31 19:42:40
想着每天能学个新东西,今天准备了这个hibernate+struts2实现页面的分页功能,以下是源代码。 1.dao层接口的设计,定义一个persondao接口,里面声明...

想着每天能学个新东西,今天准备了这个hibernate+struts2实现页面的分页功能,以下是源代码。

1.dao层接口的设计,定义一个persondao接口,里面声明了两个方法:

public interface persondao
{
  public list<person> querybypage(string hql, int offset, int pagesize);
  
  public int getallrowcount(string hql);
}

2.dao层接口的实现类persondaoimpl类,将其两个方法实现出来:

public class persondaoimpl implements persondao
{
  /**
   * 通过hql语句得到数据库中记录总数
   */
  @override
  public int getallrowcount(string hql)
  {
    session session = hibernateutil.opensession();
    transaction tx = null;
    int allrows = 0;
    try
    {
      tx = session.begintransaction();
      
      query query = session.createquery(hql);
      
      allrows = query.list().size();
      
      tx.commit();
      
    }
    catch (exception e)
    {
      if(tx != null)
      {
        tx.rollback();
      }
      
      e.printstacktrace();
    }
    finally
    {
      hibernateutil.closesession(session);
    }
    
    return allrows;
  }
  /**
   * 使用hibernate提供的分页功能,得到分页显示的数据
   */
  @suppresswarnings("unchecked")
  @override
  public list<person> querybypage(string hql, int offset, int pagesize)
  {
    session session = hibernateutil.opensession();
    transaction tx = null;
    list<person> list = null;
    
    try
    {
      tx = session.begintransaction();
      
      query query = session.createquery(hql).setfirstresult(offset).setmaxresults(pagesize);
      
      list = query.list();
      
      tx.commit();
      
    }
    catch (exception e)
    {
      if(tx != null)
      {
        tx.rollback();
      }
      
      e.printstacktrace();
    }
    finally
    {
      hibernateutil.closesession(session);
    }
    
    
    return list;
  }
}

3.定义了一个pagebean(每一页所需要的内容都存放在这个pagebean里面),里面用来存放网页每一页显示的内容:

public class pagebean
{
  private list<person> list; //通过hql从数据库分页查询出来的list集合
  
  private int allrows; //总记录数
  
  private int totalpage; //总页数
  
  private int currentpage; //当前页

  public list<person> getlist()
  {
    return list;
  }

  public void setlist(list<person> list)
  {
    this.list = list;
  }

  public int getallrows()
  {
    return allrows;
  }

  public void setallrows(int allrows)
  {
    this.allrows = allrows;
  }

  public int gettotalpage()
  {
    return totalpage;
  }

  public void settotalpage(int totalpage)
  {
    this.totalpage = totalpage;
  }

  public int getcurrentpage()
  {
    return currentpage;
  }

  public void setcurrentpage(int currentpage)
  {
    this.currentpage = currentpage;
  }
  
  /**
   * 得到总页数
   * @param pagesize 每页记录数
   * @param allrows 总记录数
   * @return 总页数
   */
  public int gettotalpages(int pagesize, int allrows)
  {
    int totalpage = (allrows % pagesize == 0)? (allrows / pagesize): (allrows / pagesize) + 1;
    
    return totalpage;
  }
  
  /**
   * 得到当前开始记录号
   * @param pagesize 每页记录数
   * @param currentpage 当前页
   * @return
   */
  public int getcurrentpageoffset(int pagesize, int currentpage)
  {
    int offset = pagesize * (currentpage - 1);
    
    return offset;
  }
  
  /**
   * 得到当前页, 如果为0 则开始第一页,否则为当前页
   * @param page
   * @return
   */
  public int getcurpage(int page)
  {
    int currentpage = (page == 0)? 1: page;
    
    return currentpage;
  }
  
}

4.service层接口设计,定义一个personservice接口,里面声明了一个方法,返回一个pagebean:

public interface personservice
{
  public pagebean getpagebean(int pagesize, int page);
}

5.service层接口实现类personserviceimpl类,实现唯一的方法:

public class personserviceimpl implements personservice
{
  private persondao persondao = new persondaoimpl();
  
  /**
   * pagesize为每页显示的记录数
   * page为当前显示的网页
   */
  @override
  public pagebean getpagebean(int pagesize, int page)
  {
    pagebean pagebean = new pagebean();
    
    string hql = "from person";
    
    int allrows = persondao.getallrowcount(hql);
    
    int totalpage = pagebean.gettotalpages(pagesize, allrows);
    
    int currentpage = pagebean.getcurpage(page);
    
    int offset = pagebean.getcurrentpageoffset(pagesize, currentpage);
    
    list<person> list = persondao.querybypage(hql, offset, pagesize);
    
    pagebean.setlist(list);
    pagebean.setallrows(allrows);
    pagebean.setcurrentpage(currentpage);
    pagebean.settotalpage(totalpage);
    
    return pagebean;
  }
}

6.action层设计,定义一个personaction:

public class personaction extends actionsupport
{
  private personservice personservice = new personserviceimpl();
  
  private int page;
  
  public int getpage()
  {
    return page;
  }

  public void setpage(int page)
  {
    this.page = page;
  }

  @override
  public string execute() throws exception
  {
    //表示每页显示5条记录,page表示当前网页
    pagebean pagebean = personservice.getpagebean(5, page);
    
    httpservletrequest request = servletactioncontext.getrequest();
    
    request.setattribute("pagebean", pagebean);
    
    return success;
  }
}

7.辅助类设计,hibernateutil:

public class hibernateutil
{
  private static sessionfactory sessionfactory;
  
  static
  {
    sessionfactory = new configuration().configure().buildsessionfactory();
  }
  
  public static session opensession()
  {
    session session = sessionfactory.opensession();
    
    return session;
  }
  
  public static void closesession(session session)
  {
    if(session != null)
    {
      session.close();
    }
  }
  
}

8.最后也就是分页页面显示pageperson.jsp:

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<html>
 <head>
  <base href="<%=basepath%>">
  
  <title>my jsp 'pageperson.jsp' starting page</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">

  <script type="text/javascript">
  
    function validate()
    {
      var page = document.getelementsbyname("page")[0].value;
        
      if(page > <s:property value="#request.pagebean.totalpage"/>)
      {
        alert("你输入的页数大于最大页数,页面将跳转到首页!");
        
        window.document.location.href = "personaction";
        
        return false;
      }
      
      return true;
    }
  
  </script>

 </head>
 
 <body>

  <h1><font color="blue">分页查询</font></h1><hr>
  
  <table border="1" align="center" bordercolor="yellow" width="50%">
  
    <tr>
      <th>序号</th>
      <th>姓名</th>
      <th>年龄</th>
    </tr>
  
  
  <s:iterator value="#request.pagebean.list" id="person">
  
    <tr>
      <th><s:property value="#person.id"/></th>
      <th><s:property value="#person.name"/></th>
      <th><s:property value="#person.age"/></th>    
    </tr>
  
  </s:iterator>
  
  </table>
  
  <center>
  
    <font size="5">共<font color="red"><s:property value="#request.pagebean.totalpage"/></font>页 </font>  
    <font size="5">共<font color="red"><s:property value="#request.pagebean.allrows"/></font>条记录</font><br><br>
    
    <s:if test="#request.pagebean.currentpage == 1">
      首页   上一页
    </s:if>
    
    <s:else>
      <a href="personaction.action">首页</a>
         
      <a href="personaction.action?page=<s:property value="#request.pagebean.currentpage - 1"/>">上一页</a>
    </s:else>
    
    <s:if test="#request.pagebean.currentpage != #request.pagebean.totalpage">
      <a href="personaction.action?page=<s:property value="#request.pagebean.currentpage + 1"/>">下一页</a>
         
      <a href="personaction.action?page=<s:property value="#request.pagebean.totalpage"/>">尾页</a>
    </s:if>
    
    <s:else>
      下一页   尾页
    </s:else>
  
  </center><br>
  
  <center>
    
    <form action="personaction" onsubmit="return validate();">
      <font size="4">跳转至</font>
      <input type="text" size="2" name="page">页
      <input type="submit" value="跳转">
    </form>
    
  </center>
  
 </body>
</html>

至此,hibernate+struts2实现网页分页功能代码部分就完毕了,像hibernate与struts的配置文件就不列出来了,那些都不是重点!

页面效果如下:

使用hibernate和struts2实现分页功能的示例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。