java struts2 hibernate分页 通用

新建一个类

Dao.java

代码:



import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


/**
 * 
 * @author Administrator
 *
 */
public class Dao extends HibernateDaoSupport{

public List getList(String hql)
{
List list = this.getHibernateTemplate().find(hql);
return list;
}

public Object get(Class cls,int id)
{
return getHibernateTemplate().get(cls, id);
}

public void update(Object obj)
{
getHibernateTemplate().update(obj);
}

public void save(Object obj)
{
getHibernateTemplate().save(obj);
}

public void delete(Object obj)
{
getHibernateTemplate().delete(obj);
}

public void update(final String hql)
{
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session){
session.createQuery(hql).executeUpdate();
return null;
}
};
getHibernateTemplate().execute(callback);
}

public PageEntity getList(final String hql, final int cp, final int ps)
{
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session){
//取总条数
String countSql = "select count(*) " + hql.substring(hql.indexOf("from "));
Object obj = session.createQuery(countSql).uniqueResult();
int countItem = new Integer(obj.toString());

//计算总页数
int countPage = (countItem-1)/ps+1;

//取开始条数
int startItem = (cp-1)*ps;
//rs = stmt.executeQuery(sql + " limit " + startItem + "," + ps);

List list = session.createQuery(hql).setFirstResult(startItem).setMaxResults(ps).list();

PageEntity pe = new PageEntity();
pe.setList(list);
pe.setCountItem(countItem);
pe.setCountPage(countPage);
pe.setPs(ps);
pe.setCp(cp);
return pe;
}
};
return (PageEntity) getHibernateTemplate().execute(callback);
}



}




再建一个类

PageEntity.java


代码:



import java.util.List;


public class PageEntity {
private List list;
private int cp;
private int countItem;
private int countPage;
private int ps;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getCp() {
return cp;
}
public void setCp(int cp) {
this.cp = cp;
}
public int getCountItem() {
return countItem;
}
public void setCountItem(int countItem) {
this.countItem = countItem;
}
public int getCountPage() {
return countPage;
}
public void setCountPage(int countPage) {
this.countPage = countPage;
}
public int getPs() {
return ps;
}
public void setPs(int ps) {
this.ps = ps;
}

}

然后建一个jsp页面

page.jsp


代码

<%@page import="com.bwie.common.PageEntity"%>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>


<body>
共${pageEntity.countItem }条${pageEntity.countPage }页 当前第${pageEntity.cp
}页 每页显示
<input type="text" id="ps" name="ps" style="width: 20px;"
value="${pageEntity.ps }">

<a href="javascript:;" οnclick="jump(1)">首页</a>
<a href="javascript:;" οnclick="jump(2)">上一页</a>
<a href="javascript:;" οnclick="jump(3)">下一页</a>
<a href="javascript:;" οnclick="jump(4)">末页</a> 跳转到
<input type="text" id="cp" name="cp" value="${pageEntity.cp }"
style="width: 20px;">

<input type="button" value="跳转" οnclick="jump(0)">
<select οnchange="jump(5,this)">
<%
PageEntity pe = (PageEntity) request.getAttribute("pageEntity");


int start = pe.getCp() - 2;
int end = pe.getCp() + 2;
if (pe.getCountPage() < 5) {
start = 1;
end = pe.getCountPage();
} else {
if (start < 2) {
start = 1;
end = 5;
}
if (end >= pe.getCountPage()) {
start = pe.getCountPage() - 4;
end = pe.getCountPage();
}
}
for (int i = start; i <= end; i++) {
%>
<option value="<%=i%>" <%if (i == pe.getCp()) {%> selected <%}%>>
第<%=i%>页
</option>
<%
}
%>
</select>
<script>
function jump(type,sel)
{
var ps = document.getElementById("ps").value;
var cp = document.getElementById("cp").value;
if (ps <= 0 || cp <= 0)
{
alert("输入数据有误");
return false;
}
if (type==0)
{
document.forms[0].submit();
}
else if (type==1)
{
document.getElementById("cp").value= 1;
document.forms[0].submit();
}
else if(type==2)
{
if (document.getElementById("cp").value>1)
{
document.getElementById("cp").value= document.getElementById("cp").value-1;
document.forms[0].submit();
}
else
{
alert('已是首页');
}

}
else if(type==3)
{
if (document.getElementById("cp").value<${pageEntity.countPage})
{
document.getElementById("cp").value= document.getElementById("cp").value*1+1;
document.forms[0].submit();
}
else
{
alert('已是末页');
}

}
else if (type==4)
{
document.getElementById("cp").value= ${pageEntity.countPage};
document.forms[0].submit();
}
else if (type==5)
{
document.getElementById("cp").value= sel.value;
document.forms[0].submit();
}
}
</script>
</body>
</html>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值