用struts实现分页

************首先创建一个Pager类,用来记录页面信息****************************
public class Pager {
private int totalRows; // 总行数

private int pageSize = 5; // 每页显示的行数

private int currentPage; // 当前页号

private int totalPages; // 总页数

private int startRow; // 当前页在数据库中的起始行

public Pager() {
}

public Pager(int _totalRows) {
   totalRows = _totalRows;
   totalPages = totalRows / pageSize;
   int mod = totalRows % pageSize;
   if (mod > 0) {
    totalPages++;
   }
   currentPage = 1;
   startRow = 0;
}

public int getStartRow() {
   return startRow;
}

public int getTotalPages() {
   return totalPages;
}

public int getCurrentPage() {
   return currentPage;
}

public int getPageSize() {
   return pageSize;
}

public void setTotalRows(int totalRows) {
   this.totalRows = totalRows;
}

public void setStartRow(int startRow) {
   this.startRow = startRow;
}

public void setTotalPages(int totalPages) {
   this.totalPages = totalPages;
}

public void setCurrentPage(int currentPage) {
   this.currentPage = currentPage;
}

public void setPageSize(int pageSize) {
   this.pageSize = pageSize;
}

public int getTotalRows() {
   return totalRows;
}

public void first() {
   currentPage = 1;
   startRow = 0;
}

public void previous() {
   if (currentPage == 1) {
    return;
   }
   currentPage--;
   startRow = (currentPage - 1) * pageSize;
}

public void next() {
   if (currentPage < totalPages) {
    currentPage++;
   }
   startRow = (currentPage - 1) * pageSize;
}

public void last() {
   currentPage = totalPages;
   startRow = (currentPage - 1) * pageSize;
}

public void refresh(int _currentPage) {
   currentPage = _currentPage;
   if (currentPage > totalPages) {
    last();
   }
}

}

**************创建PagerHelper类,用来记录当前页面信息和获得当前页面的操作***************
public class PagerHelper {

public static Pager getPager(HttpServletRequest httpServletRequest,
    int totalRows) {

   // 定义pager对象,用于传到页面
   Pager pager = new Pager(totalRows);

   // 从Request对象中获取当前页号
   String currentPage = httpServletRequest.getParameter("currentPage");

   // 如果当前页号为空,表示为首次查询该页
   // 如果不为空,则刷新pager对象,输入当前页号等信息
   if (currentPage != null) {
    pager.refresh(Integer.parseInt(currentPage));
   }

   // 获取当前执行的方法,首页,前一页,后一页,尾页。
   String pagerMethod = httpServletRequest.getParameter("pageMethod");

   if (pagerMethod != null) {
    if (pagerMethod.equals("first")) {
     pager.first();
    } else if (pagerMethod.equals("previous")) {
     pager.previous();
    } else if (pagerMethod.equals("next")) {
     pager.next();
    } else if (pagerMethod.equals("last")) {
     pager.last();
    }
   }
   return pager;
}
}

*******************创建GetHibernate类,从数据库中取得需要的数据**********************
public class GetHibernate {

public List listPage(int startRow, int pageSize) {
   CustomerDAO customerDAO = DAOFactory.getCustomerDAO();
   Session session = customerDAO.getSession();
   Query q = session.createQuery("from Customer");
   q.setFirstResult(startRow);
   q.setMaxResults(pageSize);

   List list = q.list();

   return list;
}

public int getRows() throws HibernateException {

   CustomerDAO customerDAO = DAOFactory.getCustomerDAO();
   Session session = customerDAO.getSession();

   int totalRows = 0;
   Transaction tx = null;

   try {

    tx = session.beginTransaction();

    totalRows = session.createQuery("from Customer").list().size();
    tx.commit();
   } catch (HibernateException he) {
    if (tx != null) {
     tx.rollback();
    }
    throw he;
   } finally {

    session.close();
   }

   return totalRows;
}
}

****************注意ListForm中也要记录当前页面信息,并生成get,set器***********************
private int pageSize = 5; // 每页显示的行数

private int currentPage; // 当前页号

private int totalPages; // 总页数

private int startRow; // 当前页在数据库中的起始行

**********************ListAction类将list和页面信息传给页面*******************************
public class ListAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
   GetHibernate get = new GetHibernate();

   int totalRows = get.getRows();// 记录总行数
   Pager pager = PagerHelper.getPager(request, totalRows);

   List list = get.listPage(pager.getStartRow(), pager.getPageSize());
   request.setAttribute("customers", list);

   ListForm listform = (ListForm) form;
   BeanUtils.copyProperties(listform, pager);

   request.setAttribute("pager", listform);

   ActionForward forward = mapping.findForward("success");

   return forward;
}
}


****************list.jsp页面中的翻页代码*************************************************
   <tr>
    <td colspan="3" bgcolor="#FFFFFF">第 <bean:write name="pager"
     property="currentPage" /> 页&nbsp; 每页 <bean:write name="pager"
     property="pageSize" /> 条&nbsp; 共 <bean:write name="pager"
     property="totalPages" /> 页&nbsp; <html:link
     action="list.do?pageMethod=first" paramName="pager"
     paramProperty="currentPage" paramId="currentPage">首页</html:link> <html:link
     action="list.do?pageMethod=previous" paramName="pager"
     paramProperty="currentPage" paramId="currentPage">上一页</html:link> <html:link
     action="list.do?pageMethod=next" paramName="pager"
     paramProperty="currentPage" paramId="currentPage">下一页</html:link> <html:link
     action="list.do?pageMethod=last" paramName="pager"
     paramProperty="currentPage" paramId="currentPage">尾页</html:link></td>
   </tr>


你可能感兴趣的:(jsp,struts)