Struts2+Hibernate完成數據分頁的辦法。本站提示廣大學習愛好者:(Struts2+Hibernate完成數據分頁的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Struts2+Hibernate完成數據分頁的辦法正文
本文實例講述了Struts2+Hibernate完成數據分頁的辦法。分享給年夜家供年夜家參考,詳細以下:
1.用Hibernate完成分頁技巧:
/**
* 應用hql語句停止分頁查詢
* @param hql 須要查詢的hql語句
* @param offset 第一筆記錄索引
* @param pageSize 每頁須要顯示的記載數
* @return 以後頁的一切記載
*/
@SuppressWarnings("unchecked")
public List findByPage(final String hql,
final int offset, final int pageSize)
{
//經由過程一個HibernateCallback對象來履行查詢
List list = getHibernateTemplate()
.executeFind(new HibernateCallback()
{
//完成HibernateCallback接口必需完成的辦法
public Object doInHibernate(Session session)
throws HibernateException, SQLException
{
//履行Hibernate分頁查詢
List result = session.createQuery(hql)
.setFirstResult(offset)
.setMaxResults(pageSize)
.list();
return result;
}
});
return list;
}
// 獲得總記載數
public int getRows(String hql) {
return getHibernateTemplate().find(hql).size();
}
2.在Action裡挪用Hibernate完成分頁技巧的辦法,並跳轉到顯示界面:
// 分頁
@SuppressWarnings("unchecked")
public String paging() {
String hql = "from Income"; // 分頁的數據表
int pageSize = 3; // 每頁顯示記載的條數
int allRows = service.getRows(hql); // 記載總數
int allPage = 0; // 總頁數
int offset = getPage() + 1; // 第一筆記錄的索引
/*if (rows % size != 0) {
pageSize = rows / size + 1;
} else {
pageSize = rows / size;
}*/
allPage = (allRows - 1) / pageSize + 1; // 盤算總頁數
List<Income> income = service.findByPage(hql, (offset-1)*pageSize, pageSize);
request.setAttribute("allPage", allPage);
request.setAttribute("offset", offset);
request.setAttribute("income", income);
return "paging";
}
3.struts.xml設置裝備擺設:
<action name="income"
class="com.xqh.action.IncomeAction">
<!-- 為兩個邏輯視圖設置裝備擺設視圖頁面 -->
<result name="error">/error.jsp</result>
<result name="paging">/income/income_list.jsp</result>
<result name="update">/income/income_edit.jsp</result>
</action>
4.顯示界面income_list.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<title>支出列表</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">
<link rel="stylesheet" type="text/css" href="../images/styles.css">
</head>
<body>
<div class="div1">
<table width="100%" cellpadding="0" cellspacing="0" border="0"
align="center">
<tr>
<td class="td_title1">
·以後地位:支出治理>>檢查支出
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="50">
<br>
<table border="1" align="center" width="700" cellpadding="1"
cellspacing="1" bgcolor="#036500" bordercolor="#FFFFF">
<tr bgcolor="#FFFFFF">
<td class="tb_tl" align="center">
支出編號
</td>
<td class="tb_tl" align="center">
日期
</td>
<td class="tb_tl" align="center">
方法
</td>
<td class="tb_tl" align="center">
金額
</td>
<td class="tb_tl" align="center">
項目
</td>
<td class="tb_tl" align="center">
起源
</td>
<td class="tb_tl" align="center">
人員
</td>
<td class="tb_tl" align="center">
備注
</td>
<td class="tb_tl" align="center">
操作
</td>
</tr>
<s:iterator value="#request.income">
<tr bgcolor="#FFFFFF">
<td align="center"><s:property value="id"/></td>
<td align="center"><s:date name="date" format="yyyy-MM-dd"/></td>
<td align="center"><s:property value="style"/></td>
<td align="center"><s:property value="money"/></td>
<td align="center"><s:property value="project"/></td>
<td align="center"><s:property value="source"/></td>
<td align="center"><s:property value="personnel"/></td>
<td align="center"><s:property value="remarks"/></td>
<td align="center">
<a href="javascript:if(confirm('肯定要刪除${id}嗎?'))location='income!del?id=${id}'">刪除</a>
<a href="javascript:if(confirm('肯定要修正${id}嗎?'))location='income!updateTo?id=${id}'">修正</a>
</td>
</tr>
</s:iterator>
</table>
<center>
總共有${allPage}頁, 以後是第${offset}頁
<a href="income!paging?page=0"><font size="2" color="blue">首頁</font></a>
<a href="javascript:if(${offset}>1)location='income!paging?page=${page-1}'"><font size="2" color="red">上一頁</font></a>
<a href="javascript:if(${offset}<${allPage})location='income!paging?page=${page+1}'"><font size="2" color="red">下一頁</font></a>
<a href="income!paging?page=${allPage-1}"><font size="2" color="blue">末頁</font></a>
</center>
</td>
</tr>
</table>
</div>
</body>
5.分頁成果:
本文章未供給底層數據庫中的完成,但只需控制分頁道理,信任這成績不年夜。詳細分頁道理可參照後面一篇:《Hibernate框架數據分頁技巧實例剖析》
願望本文所述對年夜家基於Hibernate框架的Java法式設計有所贊助。