亲宝软件园·资讯

展开

hibernate分页 jsp hibernate的分页代码

人气:0
想了解jsp hibernate的分页代码的相关内容吗,在本文为您仔细讲解hibernate分页的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:jsp,hibernate,分页,下面大家一起来学习吧。
可见使用Hibernate,在进行查询分页的操作上,是具有非常大的灵活性,Hibernate会首先尝试用特定数据库的分页sql,如果没用,再尝试Scrollable,如果不行,最后采用rset.next()移动的办法。
(一)pager类
* @(#)Pager.java 2005-5-3
*
* Copyright (c) 2005, Jeffrey Hsu
*/
package com.jeffrey.messagelove;
/**
* Pager holds the page info.
*/
public class Pager {
private int totalRows = 0; // 记录总数
private int totalPages = 0; // 总页数
private int pageSize = 10; // 每页显示数据条数,默认为10条记录
private int currentPage = 1; // 当前页数
private boolean hasPrevious = false; // 是否有上一页
private boolean hasNext = false; // 是否有下一页
public Pager() {
}
/**
* Initialize Pager
* @param totalRows total record rows
* @param pageSize total record is hold by every page
*/
public void init(int totalRows, int pageSize) {
this.totalRows = totalRows;
this.pageSize = pageSize;
totalPages = ((totalRows + pageSize) - 1) / pageSize;
refresh(); // 刷新当前页面信息
}
/**
* @return Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage current page
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
refresh();
}
/**
* @return Returns the pageSize.
*/
public int getPageSize() {
return pageSize;
}
/**
* @param pageSize The pageSize to set.
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
refresh();
}
/**
* @return Returns the totalPages.
*/
public int getTotalPages() {
return totalPages;
}
/**
* @param totalPages The totalPages to set.
*/
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
refresh();
}
/**
* @return Returns the totalRows.
*/
public int getTotalRows() {
return totalRows;
}
/**
* @param totalRows The totalRows to set.
*/
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
refresh();
}
// 跳到第一页
public void first() {
currentPage = 1;
this.setHasPrevious(false);
refresh();
}
// 取得上一页(重新设定当前页面即可)
public void previous() {
currentPage--;
refresh();
}
// 取得下一页
public void next() {
System.out.println("next: totalPages: " + totalPages +
" currentPage : " + currentPage);
if (currentPage < totalPages) {
currentPage++;
}
refresh();
}
// 跳到最后一页
public void last() {
currentPage = totalPages;
this.setHasNext(false);
refresh();
}
public boolean isHasNext() {
return hasNext;
}
/**
* @param hasNext The hasNext to set.
*/
public void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
public boolean isHasPrevious() {
return hasPrevious;
}
/**
* @param hasPrevious The hasPrevious to set.
*/
public void setHasPrevious(boolean hasPrevious) {
this.hasPrevious = hasPrevious;
}

// 刷新当前页面信息
public void refresh() {
if (totalPages <= 1) {
hasPrevious = false;
hasNext = false;
} else if (currentPage == 1) {
hasPrevious = false;
hasNext = true;
} else if (currentPage == totalPages) {
hasPrevious = true;
hasNext = false;
} else {
hasPrevious = true;
hasNext = true;
}
}
}
Action一:
public ActionForward queryWithPage(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletresponse) {
Collection clInfos = null;//用于输出到页面的记录集合
int totalRows;//记录总行数
VehiclePropertyDAO vehicleDAO = new VehiclePropertyDAO();
//取得当前表中的总行数
try {
totalRows = vehicleDAO.getRows("select count(*) from VehicleProperty");
} catch (Exception ex) {
servlet.log(ex.toString());
return actionMapping.findForward(Constants.FAILURE);
}
//通过PagerHelper类来获取用于输出到页面的pager对象
Pager pager=PagerHelper.getPager(httpServletRequest,totalRows);
//取出从startRow开始的pageSize行记录
try {
clInfos = vehicleDAO.findWithPage(pager.getPageSize(), pager.getStartRow());
} catch (Exception ex) {
servlet.log(ex.toString());
return actionMapping.findForward(Constants.FAILURE);
}
//把输出的记录集和pager对象保存到request对象中
httpServletRequest.setAttribute("CLINFOS", clInfos);
httpServletRequest.setAttribute("PAGER", pager);
return actionMapping.findForward(Constants.SUCCESS);
}
查询语句select count(*) from VehicleProperty 也可以换成你需要的任意的条件(select count(*) from VehicleProperty where ..)
Action二:
DisplayAllAction 显示数据页面控制器
package com.jeffrey.messagelove;
import com.jeffrey.messagelove.*;
import com.jeffrey.messagelove.Pager;
import com.jeffrey.messagelove.hibernate.*;
/*
* @(#)DisplayAction.java 2005-5-2
*
* Copyright (c) 2005, Jeffrey Xu
*/
import org.apache.struts.action.*;
import java.util.*;
import javax.servlet.http.*;
/**
* 显示页面控制器
*/
public class DisplayAllAction extends Action {
private HibernateDAO hibernateDAO = new HibernateDAO();
private Pager pager = new Pager();
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
List messageList = null;
HQuery hquery = new HQuery();
int totalRows = 0;
int startRow = 0;
try {
totalRows = hibernateDAO.getRows("select count(*) from Message");
// 初始化页面信息
pager.init(totalRows, Constants.RECORD_PER_PAGE);
} catch (Exception ex) {
ex.printStackTrace();
}
hquery.setQueryString("From Message order by m_sendDate desc");
String viewPage = (String) request.getParameter("viewPage");
String action = (String) request.getParameter("action");
// 跳转至相应页面
if (viewPage != null && !viewPage.equals("")) {
try {
pager.setCurrentPage(Integer.parseInt(viewPage));
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
if (action != null) {
// 根据传递进来的参数控制页面的前进后退
if (action.equalsIgnoreCase("previous")) {
pager.previous();
} else if (action.equalsIgnoreCase("next")) {
pager.next();
} else if (action.equalsIgnoreCase("first")) {
pager.first();
} else if (action.equalsIgnoreCase("last")) {
pager.last();
}
}
try {
hquery.setPageStartNo(pager.getCurrentPage());
messageList = hibernateDAO.find(hquery);
} catch (Exception ex) {
ex.printStackTrace();
}
request.setAttribute("list", messageList);
session.setAttribute("pager", pager);
return mapping.findForward("display");
}
}
Action中PagerHelp类以及DAO
package com.jpcf.db.helper;
import javax.servlet.http.*;
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;
}
}
DAO类
package com.jpcf.db.dao;
import com.jpcf.db.model.*;
import com.jpcf.db.helper.HibernateUtil;
import net.sf.hibernate.*;
import java.util.*;
import com.jpcf.db.controller.*;
public class VehiclePropertyDAO {
public Collection findWithPage(int pageSize, int startRow) throws
HibernateException {
Collection vehicleList = null;
Transaction tx = null;
try {
Session session = HibernateUtil.currentSession();
tx = session.beginTransaction();
Query q = session.createQuery("from VehicleProperty vp");
q.setFirstResult(startRow);
q.setMaxResults(pageSize);
vehicleList = q.list();
tx.commit();
} catch (HibernateException he) {
if (tx != null) {
tx.rollback();
}
throw he;
} finally {
HibernateUtil.closeSession();
}
return vehicleList;
}
public int getRows(String query) throws
HibernateException {
int totalRows = 0;
Transaction tx = null;
try {
Session session = HibernateUtil.currentSession();
tx = session.beginTransaction();
totalRows = ((Integer) session.iterate(query).next()).
intValue();
tx.commit();
} catch (HibernateException he) {
if (tx != null) {
tx.rollback();
}
throw he;
} finally {
HibernateUtil.closeSession();
}
return totalRows;
}
}

jsp页面一:
第页
共页
="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=
first
上一页
下一页
尾页
" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">
首页
解释一下这一行:"/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first
method=queryWithPage 是由于我的Action继承的是DispatchAction,需要一个method参数
pageMethod=first 是用来在PageHelper类中判断执行哪个操作
jsp页面二:
<%-- 分页导航 --%>
<table border="0" width="780" class="pageInfo">
<tr>
<td width="250">
共 <bean:write name="pager" property="totalRows"/><span class="pageInfo">条记录 </span>
第 <bean:write name="pager" property="currentPage"/>/<bean:write name="pager" property="totalPages"/>页
</td>
<td align="right" width="60">
<html:link page="/Displayall.do?viewPage=&action=first">首页</html:link>
</td>
<td align="right" width="60">
<logic:equal name="pager" property="hasPrevious" value="true">
<html:link page="/Displayall.do?viewPage=&action=previous">上一页</html:link>
</logic:equal>
<logic:equal name="pager" property="hasPrevious" value="false">
<span class="invalidLink">上一页</span>
</logic:equal>
</td>
<td align="center" width="4">

</td>
<td align="left" width="60">
<logic:equal name="pager" property="hasNext" value="true" >
<html:link page="/Displayall.do?viewPage=&action=next">下一页</html:link>
</logic:equal>
<logic:equal name="pager" property="hasNext" value="false">
<span class="invalidLink">下一页</span>
</logic:equal>
</td>
<td width="60">
<html:link page="/Displayall.do?viewPage=&action=last">末页</html:link>
</td>
<td width="160" align="rigth">
<%-- 跳转相应页面,参见下文页面跳转部分 --%>
<html:form action="/ViewPage.do">
跳转到
<html:text property="targetPage" size="3" maxlength="3"/>
<html:submit value="GO"/>
</html:form>
</td>
<td>
</td>
</tr>
</table>
<%-- 分页导航结束 --%>

  

加载全部内容

相关教程
猜你喜欢
用户评论