Jpage分页+Hibernate+Ajax+jquery+JSON通用技术<一>

首先:

1.工具类page:

package com.common.util;

import java.util.List;

@SuppressWarnings("unchecked")
public class Page
{
private int totalRows; // 总行数
private int pageSize; // 每页显示的行数
private int currentPage; // 当前页号
private int totalPages; // 总页数
private int startRow; // 当前页在数据库中的起始行
private List list;
public Page(int _totalRows, int _pageSize, int _currentPage)
{
   totalRows = _totalRows;
   pageSize = _pageSize;
   currentPage = _currentPage;
   // 计算总页数
   totalPages = totalRows / pageSize;
   int mod = totalRows % pageSize;
   if (mod > 0)
   {

totalPages++;

}
   currentPage = 1;
   startRow = 0;
}

public int getCurrentPage()
{
   return currentPage;
}

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

/**
* 获得每页显示的行数
*
* @return
*/
public int getPageSize()
{
   return pageSize;
}

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

/**
* 获得当前页在数据库中的起始行
*
* @return
*/
public int getStartRow()
{
   return startRow;
}

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

public int getTotalPages()
{
   return totalPages;
}

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

public int getTotalRows()
{
   return totalRows;
}

public void setTotalRows(int totalRows)
{
   this.totalRows = 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 any()
{
   if (currentPage < 1)
   {
    currentPage = 1;
   }
   if (currentPage > totalPages)
   {
    currentPage = totalPages;
   }
   startRow = (currentPage - 1) * pageSize;
}

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

public List getList() {
   return list;
}

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

 


2.工具类pageHelp:

package com.common.util;

import javax.servlet.http.HttpServletRequest;

public class PageHelp
{

public static Page getPage(HttpServletRequest request, int totalRows,
    int pageSize, int currentPage)
{
   Page page = new Page(totalRows, pageSize, currentPage);
   // 获得当前页号
   String pagecount = currentPage + "";
  
   // 如果当前页号为空,表示为首次查询该页
   // 如果不为空,则刷新page对象,输入当前页号等信息
   if (pagecount != null)
   {
    page.refresh(Integer.parseInt(pagecount));
   }
  
   // 获取当前执行的方法,首页,前一页,后一页,尾页。
   String pagerMethod = "any";
   if (request.getParameter("pageMethod") != null && !"undefined".equals(request.getParameter("pageMethod")))
   {
    pagerMethod = request.getParameter("pageMethod");
   }
  
   if (pagerMethod != null)
   {
    if (pagerMethod.equals("first"))
    {
     page.first();
    }
    else if (pagerMethod.equals("previous"))
    {
     page.previous();
    }
    else if (pagerMethod.equals("next"))
    {
     page.next();
    }
    else if (pagerMethod.equals("last"))
    {
     page.last();
    }
    else
    {
     page.any();
    }
   }
   return page;
}
}


3.ClientDAOl类:

以客户列表为例:

public int getTotal()
{
   String hql = "select count(*) from Client";
   Query query = getSession().createQuery(hql);
   return Integer.valueOf(query.uniqueResult().toString());
}

@SuppressWarnings("unchecked")
public List findByPage(int startRow, int pageSize)
{
   Criteria cri = getSession().createCriteria(client.clazz);
   cri.setFirstResult(startRow);
   cri.setMaxResults(pageSize);
   return cri.list();
}


4.ClientBIZ类:

public int getTotal()
{
   return clientDAO().getTotal();
}
public List findByPage(int startRow, int pageSize)
{
   return clientDAO().findByPage(startRow, pageSize);
}

 


5.Action类:
public Page getPage(ClientBiz biz, HttpServletRequest request)
{
   int totalRows = biz.getTotal();
  
   int pageSize = 5;
   if (request.getParameter("pageSize") != null && !"undefined".equals(request.getParameter("pageSize")))
   {
    pageSize = Integer.parseInt(request.getParameter("pageSize"));
   }
  
   int currentPage = 1;
   if (request.getParameter("currentPage") != null && !"undefined".equals(request.getParameter("currentPage")))
   {
    currentPage = Integer.parseInt(request.getParameter("currentPage"));
   }
  
   Page page = PageHelp.getPage(request, totalRows, pageSize, currentPage);
   List list = biz.findByPage(page.getStartRow(), page.getPageSize());
  
   if (list != null || !list.isEmpty())
   {
    page.setList(list);
   }
   return page;
}

//使用Ajax技术执行此方法
public void dolist(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
   Page page = getPage(clientBiz, request);
   JsonConfig config = new JsonConfig();
   config.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
   JSONObject json = JSONObject.fromObject(page,config);
   print(json.toString(), response);
}

你可能感兴趣的:(jquery,Ajax,json,Hibernate,qq)