日期:2014-05-17 浏览次数:20838 次
import java.util.List; import org.apache.struts2.ServletActionContext; import org.hibernate.HibernateException; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class Zmxpage extends HibernateDaoSupport { private int first;//从哪开始 private int pageSize;//一页存在多少数据 private String hql;//查询语句 private String hql1; public String getHql1() { return hql1; } public void setHql1(String hql1) { this.hql1 = hql1; } /* * ???磺Zmxpage z = new Zmxpage(); * z.getPages(1,10"from BeanName"); * List list = z.findByPage(); */ // 分页方法 public void getPages(int currentPage,int pageSize,String hql) { this.first = (currentPage-1)*pageSize; this.pageSize = pageSize; this.hql = hql; } public List findByPage() { try { List result = (List)getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session sess)throws HibernateException { List tmp = sess.createQuery(hql) .setFirstResult(first) .setMaxResults(pageSize) .list(); return tmp; } }); return result; } catch (RuntimeException re) { throw re; } } public int gettotalPages() {//获取总页数 try { List list = getHibernateTemplate().find("select count(*) "+hql); int j=0; j = ((Long)list.get(0)).intValue(); int i = 0; if(j%pageSize==0) i = j/pageSize; else i = j/pageSize+1; return i; } catch (RuntimeException re) { throw re; } } }
------解决方案--------------------
jsp页面
<tr>
<td colspan="5" align="center">
<s:a href="rybxx!show?currentpage=1&name=%{name}&sex=%{sex}">首页|</s:a>
<s:if test="%{currentpage>1}">
<s:a href="rybxx!show?currentpage=%{currentpage-1}&name=%{name}&sex=%{sex}">上一页|</s:a>
</s:if>
<s:if test="%{currentpage<pages}">
<s:a href="rybxx!show?currentpage=%{currentpage+1}&name=%{name}&sex=%{sex}">下一页|</s:a>
</s:if>
<s:a href="rybxx!show?currentpa