Struts2+Hibernate实现数据分页的方法
作者:TKD03072010 时间:2022-10-28 05:31:13
本文实例讲述了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程序设计有所帮助。
标签:Struts2,Hibernate,分页
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
shiro整合springboot前后端分离
2022-02-22 10:40:31
JetBrains IntelliJ IDEA 配置优化技巧
2022-06-19 08:31:38
![](https://img.aspxhome.com/file/2023/7/64067_0s.png)
Android Gradle多渠道打包的实现方法
2023-04-26 17:03:26
![](https://img.aspxhome.com/file/2023/3/87333_0s.png)
基于ClasspathResource路径问题的解决
2022-03-29 21:23:22
![](https://img.aspxhome.com/file/2023/6/66536_0s.png)
什么是递归?用Java写一个简单的递归程序
2022-02-11 19:39:45
![](https://img.aspxhome.com/file/2023/2/60772_0s.jpg)
springboot操作静态资源文件的方法
2022-07-13 06:29:11
C#与C++ dll之间传递字符串string wchar_t* char* IntPtr问题
2022-09-16 09:34:53
详解Java目录操作与文件操作教程
2023-12-05 13:45:48
![](https://img.aspxhome.com/file/2023/7/58887_0s.png)
分享两种实现Winform程序的多语言支持的多种解决方案
2023-10-17 21:24:17
SpringCloud如何解决服务之间的通信问题
2023-03-27 03:13:05
![](https://img.aspxhome.com/file/2023/2/72552_0s.jpg)
Java中的Object类详细介绍
2023-11-23 23:18:46
java调用百度的接口获取起-止位置的距离
2023-08-11 17:14:27
![](https://img.aspxhome.com/file/2023/8/70928_0s.png)
Java遍历Map对象的四种方式
2022-05-07 14:30:04
Spring Boot启动端口修改方法
2022-11-21 19:37:53
C++程序中启动线程的方法
2023-06-28 03:35:02
SpringBoot应用War包形式部署到外部Tomcat的方法
2021-10-06 04:59:48
![](https://img.aspxhome.com/file/2023/4/73024_0s.png)
C#实现收发邮件功能
2021-09-20 19:40:46
Spring Boot 集成Redisson实现分布式锁详细案例
2023-06-10 05:09:17
![](https://img.aspxhome.com/file/2023/5/83485_0s.png)
在.NET中读取嵌入和使用资源文件的方法
2022-03-28 21:46:14
![](https://img.aspxhome.com/file/2023/0/80050_0s.gif)
SpringBoot Web详解静态资源规则与定制化处理
2022-01-23 16:19:57
![](https://img.aspxhome.com/file/2023/6/80966_0s.png)