Hibernate+struts2+spring 进行分页

首先定义一个包含分页相关信息的分页类PageResultSet类:

package cn.tutorinfo.pagination;

import java.util.List;

/**
 *该类描述了一个分页数据集 list中是查询的数据集合 ,pageInfo则描述了附加的页相关的信息
 */

public class PageResultSet<T> {
 private List<T> list;  //当前页的数据信息
 private PageInfo pageInfo; //当前页的信息

 public List<T> getList() {
  return list;
 }

 public void setList(List<T> list) {
  this.list = list;
 }

 public PageInfo getPageInfo() {
  return pageInfo;
 }

 public void setPageInfo(PageInfo pageInfo) {
  this.pageInfo = pageInfo;
 }

} 其中关于页的信息类PageInfo 定义如下:

package cn.tutorinfo.pagination;

/**
 * 该类描述了分页记录集中的关于页的信息
 */

public class PageInfo {
 private int totalRow; //总共记录数
 private int totalPage; //总共页数
 private int currentPage = 1; //当前页,默认为1
 private int pageSize = 20;   //页的大小
 private boolean hasPrevious;
 private boolean hasNext;
 private boolean bof;
 private boolean eof;

    /*
  * 构造方法
    @param totalRow 总记录数
    @param pageSize 页的大小
    @param page 页码
     */
 public PageInfo(int totalRow, int pageSize, int page) {
  this.totalRow = totalRow;
  this.pageSize = pageSize;

  // 根据页大小和总记录数计算出总页数
  this.totalPage = countTotalPage(this.pageSize, this.totalRow);
  
  // 修正当前页
  setCurrentPage(page);

  init();
 }

 public int getTotalPage() {
  return totalPage;
 }

 public int getCurrentPage() {
  return this.currentPage;
 }

 // 修正计算当前页
 public void setCurrentPage(int currentPage) {
        if(currentPage>this.totalPage){
         this.currentPage=this.totalPage;         
        }else if (currentPage<1){
         this.currentPage=1;
        }
        else{
         this.currentPage=currentPage;
        }

 }

 // 获取分页大小
 public int getPageSize() {
  return pageSize;
 }

 // 设置分页大小
 public void setPageSize(int pageSize) {
  this.pageSize = pageSize;
 }

 // 获取当前页记录的开始索引
 public int getBeginIndex() {
  int beginIndex = (currentPage - 1) * pageSize; // 索引下标从0开始
  return beginIndex;
 }

 // 计算总页数
 public int countTotalPage(int pageSize, int totalRow) {
  int totalPage = totalRow % pageSize == 0 ? totalRow / pageSize : totalRow / pageSize + 1;
  return totalPage;
 }

 // 返回下一页的页码
 public int getNextPage() {
  if (currentPage + 1 >= this.totalPage) { // 如果当前页已经是最后页 则返回最大页
   return this.totalPage;
  }
  return currentPage + 1;
 }

 // 返回前一页的页码
 public int getPreviousPage() {
  if (currentPage - 1 <= 1) {
   return 1;
  } else {
   return currentPage - 1;
  }
 }

 public boolean isHasPrevious() {
  return hasPrevious;
 }

 public boolean isHasNext() {
  return hasNext;
 }

 public boolean isBof() {
  return bof;
 }

 public boolean isEof() {
  return eof;
 }

 public boolean hasNext() {
  return currentPage < this.totalPage;
 }

 public boolean hasPrevious() {
  return currentPage > 1;
 }

 public boolean isFirst() {
  return currentPage == 1;
 }

 public boolean isLast() {
  return currentPage >= this.totalPage;
 }

 // 初始化信息
 private void init() {
  this.hasNext = hasNext();
  this.hasPrevious = hasPrevious();
  this.bof = isFirst();
  this.eof = isLast();
 }

}
 

Dao接口层代码:

package cn.tutorinfo.dao;

import java.util.List;
import cn.tutorinfo.domain.Teacher;


public interface TeacherDao {

 
 //分页查询显示
 public List<Teacher> queryByPage(String hql,int beginIndex,int pageSize);
 
 //查询的结果记录总数
 public int queryRowCount(final String hql);

}

DAO实现层代码:

/*
  * 分页查询显示
  * @param hql查询的的hql语句
  * @param beginIndex 查询记录的起始索引位置
  * @param pageSize 一次查询记录个数
  * @return List<Teacher>
  */
 @SuppressWarnings("unchecked")
 public List<Teacher> queryByPage(String hql, int beginIndex, int pageSize) {
  Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
  Transaction transaction = null;
  try {
   transaction = session.beginTransaction();
   Query query = session.createQuery(hql);
   query.setFirstResult(beginIndex);
   query.setMaxResults(pageSize);
   List<Teacher> list = query.list();
   transaction.commit();
   return list;
  } catch (Exception e) {
   if (null != transaction) {
    transaction.rollback();
    e.printStackTrace();
   }
  }
  return null;
 }

 

      / / 查询指定HQL的结果记录总数
 public int queryRowCount(final String hql) {
  Session session=HibernateSessionFactory.getSessionFactory().getCurrentSession();
  Transaction transaction=null;
  try{
   transaction=session.beginTransaction();
   Query query=session.createQuery(hql);
   int result=query.list().size();  //计算个数
   transaction.commit();
   return result;
  }catch (Exception e){
   if (null!=transaction){
    transaction.rollback();
    e.printStackTrace();
   }
  }
  return 0;
 }

业务逻辑层接口:

package cn.tutorinfo.service;

import java.util.List;

import cn.tutorinfo.domain.Teacher;
import cn.tutorinfo.pagination.PageResultSet;

public interface TeacherService {
 
 /*
  * @param pagesize 每页大小
  * @param page 要显示数据的页
  * @return 包含结果集和分页信息的数据集
  */
 public PageResultSet<Teacher> queryByPage(int pageSize,int Page);
}

业务逻辑实现类:

//分页获取记录值
 public PageResultSet<Teacher> queryByPage(int pageSize, int page) {
  
     
  String hql = "From Teacher";  //查询HQL语句
  int totalRow = teacherDao.queryRowCount(hql); // 计算总记录个数
  PageInfo pageinfo = new PageInfo(totalRow, pageSize, page);
  
  //获取该页的记录
  List<Teacher> list = teacherDao.queryByPage(hql, pageinfo.getBeginIndex(), pageinfo.getPageSize());
 
  PageResultSet<Teacher> pageResultSet = new PageResultSet<Teacher>();
  pageResultSet.setList(list);
  pageResultSet.setPageInfo(pageinfo);
  return pageResultSet;
 }

struts2 的action中调用业务逻辑实现类来获取页的信息:

package cn.tutorinfo.struts;

import cn.tutorinfo.domain.Teacher;
import cn.tutorinfo.pagination.PageResultSet;
import cn.tutorinfo.service.TeacherService;

import com.opensymphony.xwork2.ActionSupport;

public class ListTeacherAction extends ActionSupport {

 private static final long serialVersionUID = -7735757614996395820L;
 private int page;  //接受传递的页码参数
 private PageResultSet<Teacher> pageResultSet;
 private TeacherService service; // 业务逻辑组件类由Spring注入

 public int getPage() {
  return page;
 }

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

 public PageResultSet<Teacher> getPageResultSet() {
  return pageResultSet;
 }

 public void setPageResultSet(PageResultSet<Teacher> pageResultSet) {
  this.pageResultSet = pageResultSet;
 }

 public void setService(TeacherService service) {
  this.service = service;
 }

 @Override
 public String execute() throws Exception {  
  this.pageResultSet = service.queryByPage(15,page); // 显示第page页,显示15条记录
  return SUCCESS;
 }

}
 

 

业务逻辑实现类中的Dao和Action中的业务逻辑组件类都由spring注入.

 

okay,以上基本完成.前台jsp页面来显示经过action处理后的分页信息:

  <!--正文标题的内容-->
             <tr>
              <td>
       <table width="100%" border="0" cellspacing="1" cellpadding="0" class="thin">
       <thead>
       <tr>
            <th width="5%"><input type="checkbox" name="checkedAll" id="checkedAll"/></th>
            <th width="5%">编号</th>
            <th width="20%">讲师姓名</th>
            <th width="10%">性别</th>
            <th width="20%">讲师类别</th>
            <th widht="20%">讲师介绍</th>
            <th width="20%">基本操作</th>
       </tr>
       </thead>
                <tbody>
                <s:iterator value="#request.pageResultSet.list" id="teacher">
                <tr>
                   <td><input type="checkbox" name="teacherid" id="teacherid" value="${teacher.id}" /></td>
                   <td><s:property value="#teacher.id" /></td>
                   <td><s:property value="#teacher.name" /></td>
                   <td><s:property value="#teacher.sex" /></td>
                   <td><s:property value="#teacher.teacherType.teacherType" /></td>
                   <td>讲师详细介绍</td>
                   <td><a href="/course/admin/editTeacher.action?id=${teacher.id}">修改</a> |
                       <a href="/course/admin/deleteTeacher.action?id=${teacher.id}" onclick="javascript:return del()">删除 </a></td>  
               </tr>
               </s:iterator>
               </tbody>
              </table>
   </td>
   </tr>
   
   <!--分页信息显示-->
             <tr>
               <td>
      <table width="100%" border="0" cellpadding="0" cellspacing="0" class="line_table">
                <tr>
                   <td align="left">选择: 全选 - 反选 - 不选 </td>
                   <td align="right">
                                                     页码:&nbsp;&nbsp;<s:property value="pageResultSet.pageInfo.currentPage" />&nbsp;/&nbsp;<s:property value="pageResultSet.pageInfo.totalPage"/> &nbsp;&nbsp;&nbsp;                                      
                   <s:if test="pageResultSet.pageInfo.bof">
                                                             首页    第一页
                   </s:if>
                   <s:else>
                     <a href="listTeacher.action?page=1">首页</a> <a href="listTeacher.action?page=<s:property value="pageResultSet.pageInfo.currentPage-1"/>">上一页</a>
                   </s:else>
                  
                   <s:if test="pageResultSet.pageInfo.eof">
                                                         下一页   末页
                   </s:if>
                   <s:else>                                                             
                      <a href="listTeacher.action?page=<s:property value="pageResultSet.pageInfo.currentPage+1"/>">下一页</a> <a href="listTeacher.action?page=<s:property value="pageResultSet.pageInfo.totalPage"/>">末页</a>
                   </s:else>
                   </td>
                </tr>
               </table>
      </td>
              </tr>

调用: http://localhost:8080/appName/listTeacher.action?page=2

 

则显示所有教师信息中的第二页中的全部数据 

posted @ 2010-04-07 11:23  游游鸟  阅读(679)  评论(0编辑  收藏  举报