struts2+hibernate分页
来源:互联网 发布:安知玉如意书包网 编辑:程序博客网 时间:2024/06/04 08:07
摘抄一:
操作数据: PageDao.java
package com.enorth.lichen.youth.dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import com.enorth.lichen.youth.hi.HibernateUtils;
public class PageDao {
public List pageQuery(int pageSize, int pageNow){
List list=new ArrayList();
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//得到每页显示的LevTwoInFuUser的对象的集合list
list = session.createQuery("from LevTwoInFuUser l2if where l2if.permission = 201 order by l2if.l2infid")
.setFirstResult(pageNow*pageSize-pageSize)
.setMaxResults(pageSize)
.list();
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
return list;
}
摘抄二:
struts2 分页
首先写了一个分页的类,其实只有主要属性的setter和getter方法
/**
* 分页类
* @author qinglin876
*
*/
public class Pagination {
private int start;
//一次取得的数量
private int size;
//要取得页数
private int currentPage = 1;
//总的记录页数
private int totalPage = 0;
//总的记录条数
private int totalRecord;
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
}
public Pagination(){
}
public Pagination(int size){
this.size = size;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
}
另外pagination.jsp由pagination类填充
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<SCRIPT type="text/javascript">
function trim(str){
return str.replace(/(^/s*)|(/s*$)/g, "");
}
function selectPage(input){
var value = trim(input.value);
if(value == ""){
return;
}
if(//d+/.test(value)){
input.form.submit();
return;
}
alert("请输入正确的页数");
input.focus();
}
</SCRIPT>
<div class="pagech">
<s:if test="pagination.totalPage != 0">
<s:url action="%{#request.url}" id="first">
<s:param name="pagination.currentPage" value="1"></s:param>
</s:url>
<s:url action="%{#request.url}" id="next" >
<s:param name="pagination.currentPage"
value="pagination.currentPage+1">
</s:param>
</s:url>
<s:url action="%{#request.url}" id="prior" >
<s:param name="pagination.currentPage"
value="pagination.currentPage-1"></s:param>
</s:url>
<s:url action="%{#request.url}" id="last">
<s:param name="pagination.currentPage" value="pagination.totalPage"></s:param>
</s:url>
<s:if test="pagination.currentPage == 1">
<span class="current">首页</span>
<span class="current">上一页</span>
</s:if>
<s:else>
<s:a href="%{first}">首页</s:a>
<s:a href="%{prior}">上一页</s:a>
</s:else>
<s:if
test="pagination.currentPage == pagination.totalPage || pagination.totalPage == 0">
<span class="current">下一页</span>
<span class="current">末页</span>
</s:if>
<s:else>
<s:a href="%{next}">下一页</s:a>
<s:a href="%{last}">末页</s:a>
</s:else>
<span class="jumplabel">跳转到</span>
<s:form action="%{#request.url}" theme="simple"
cssStyle="display:inline">
<s:hidden name="pagination.totalPage" value="%{pagination.totalPage}"></s:hidden>
<input type="text" name="pagination.currentPage" size="2"
onblur="selectPage(this)" />
</s:form>
<span class="jumplabel">页</span>
<span class="jumplabel">共<s:property
value="pagination.totalRecord" />条</span>
<span class="jumplabel">当前是第<s:property
value="pagination.currentPage" />/<s:property value="pagination.totalPage"/>页</span>
</s:if>
</div>
用的时候,在页面include进去,注意上面的"%{#request.url}",即是在struts2的action里面有一个setter和getter方法,下面看action中的某个方法
public String showNotices() throws Exception{
if(tip != null){
tip = new String(tip.getBytes("iso8859-1"),"utf-8");
}
if(notices == null)
this.notices = new ArrayList<Notice>();
int size = Integer.valueOf(this.getText("per_page_notice_size"));
if (pagination == null) {
pagination = new Pagination(size);
}
pagination.setSize(size);
if (pagination.getCurrentPage() <= 0) {
pagination.setCurrentPage(1);
}
if (pagination.getTotalPage() != 0
&& pagination.getCurrentPage() > pagination.getTotalPage()) {
pagination.setCurrentPage(pagination.getTotalPage());
}
url = "goto_showNotices.action"; this.notices.addAll(this.noticeDAO.showAll(pagination));
if(this.notices.size() == 0 && pagination.getCurrentPage() != 1){
pagination.setCurrentPage(pagination.getCurrentPage()-1);
this.notices.addAll(this.noticeDAO.showAll(pagination));
}
return "success";
}
在上面的this.noticeDAO.showAll(pagination))中填充pagination,具体如下
/*
* 显示所有的通告
* @see com.qinglin.dao.NoticeDAO#showAll(com.qinglin.util.Pagination)
*/
@SuppressWarnings("unchecked")
public List<Notice> showAll(final Pagination pagination) {
String hql = "from Notice as n";
this.getHibernateTemplate().setCacheQueries(true);
int totalRecord = ((Long) this.getSession().createQuery(
"select count(*) " + hql).uniqueResult()).intValue();
int totalPage = totalRecord % pagination.getSize() == 0 ? totalRecord
/ pagination.getSize() : totalRecord / pagination.getSize() + 1;
pagination.setTotalRecord(totalRecord);
pagination.setTotalPage(totalPage);
hql += " order by n.add_date desc";
final String hql1 = hql;
return (List<Notice>) this.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql1);
query.setFirstResult((pagination.getCurrentPage() - 1)
* pagination.getSize());
query.setMaxResults(pagination.getSize());
return query.list();
}
});
}
基本上就这些,当然请求的action里面需要设置pagination的setter和getter方法
这个分页方法特点是简单,只需在action中指明请求的url,用某种方法填充pagination,在显示的页面包含pagination.jsp即可。
- struts2+spring+hibernate分页
- struts2+hibernate分页
- struts2+Hibernate分页
- Struts2+Hibernate分页查询
- Struts2.0 +Hibernate 分页
- HIbernate+struts2分页案例
- Struts2+HIBERNATE实现分页
- Struts2+HIBERNATE实现分页
- struts2+hibernate+spring 分页
- hibernate struts2分页显示
- Hibernate+Struts2 分页实现
- Struts2 + Hibernate 分页程序实例
- Struts2+Hibernate分页显示实例
- struts2+hibernate+spring 分页制作
- struts2+hibernate+spring 分页制作
- struts2+hibernate+spring分页实现
- struts2+hibernate+spring分页实现
- struts2、hibernate 分页(自定义标签)
- Spring中获得bean的方法
- Using Variant to Store Point
- 1分钟学会使用Tor代理教程
- 几种获得spring里注册Bean的方法
- C++虚函数表解析
- struts2+hibernate分页
- Visual Studio 2005(2008)更改App_Code.dll名称
- 饮品店:单价低于5元有市场
- Digital Forensics Framework v0.4.3 available
- XNA开发软件硬件环境介绍及下载链接
- 文本域获得焦点置到内容的最后
- 通过网络映射进行增量备份
- 指针详解
- How to deal with generation gap