struts2+spring2.5+hibernate3整合分页的例子
其次在eclpise下整合框架ssh2,这里就不写整合的部分了
下面开始程序:
创建web工程,在src下创建dao接口:
public interface CoursesUserDao {
/** *//**
* 分页查询
* @param hql 查询的条件
* @param offset 开始记录
* @param length 一次查询几条记录
* @return
*/
public List queryForPage(final String hql,final int offset,final int length);
public int getAllRowCount(String hql);
}
实现dao接口类:
public class CoursesUserDaoImp extends HibernateDaoSupport implements CoursesUserDao {
/**
* 分页查询
* **/
public List queryForPage(final String hql,final int offset,final int length){
List list = this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException,SQLException{
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
}
);
return list;
}
public int getAllRowCount(String hql){
return getHibernateTemplate().find(hql).size();
}
}
创建service接口:
public interface CoursesUserService {
//分页
public PageBean queryForPage(int pageSize,int currentPage);
}
实现servcie接口类:
public class CoursesUserServiceImp implements CoursesUserService {
//分页部分:
/** *//**
* 分页查询
* @param currentPage 当前第几页
* @param pageSize 每页大小
* @return 封闭了分页信息(包括记录集list)的Bean
*/
@SuppressWarnings(“unchecked”)
public PageBean queryForPage(int pageSize,int page){
final String hql = “from Courses”; //查询语句
int allRow = this.cudao.getAllRowCount(hql); //总记录数
int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数
final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录
final int length = pageSize; //每页记录数
final int currentPage = PageBean.countCurrentPage(page);
List<Courses> list = this.cudao.queryForPage(hql,offset, length); //”一页”的记录
//把分页信息保存到Bean中
PageBean pageBean = new PageBean();
pageBean.setPageSize(pageSize);
pageBean.setCurrentPage(currentPage);
pageBean.setAllRow(allRow);
pageBean.setTotalPage(totalPage);
pageBean.setList(list);
pageBean.init();
return pageBean;
}
}
创建分页bean:
import java.util.List;
public class PageBean {
private List list; //要返回的某一页的记录列表
private int allRow; //总记录数
private int totalPage; //总页数
private int currentPage; //当前页
private int pageSize; //每页记录数
private boolean isFirstPage; //是否为第一页
private boolean isLastPage; //是否为最后一页
private boolean hasPreviousPage; //是否有前一页
private boolean hasNextPage; //是否有下一页
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getAllRow() {
return allRow;
}
public void setAllRow(int allRow) {
this.allRow = allRow;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/** *//**
* 初始化分页信息
*/
public void init(){
this.isFirstPage = isFirstPage();
this.isLastPage = isLastPage();
this.hasPreviousPage = isHasPreviousPage();
this.hasNextPage = isHasNextPage();
}
/** *//**
* 以下判断页的信息,只需getter方法(is方法)即可
* @return
*/
public boolean isFirstPage() {
return currentPage == 1; // 如是当前页是第1页
}
public boolean isLastPage() {
return currentPage == totalPage; //如果当前页是最后一页
}
public boolean isHasPreviousPage() {
return currentPage != 1; //只要当前页不是第1页
}
public boolean isHasNextPage() {
return currentPage != totalPage; //只要当前页不是最后1页
}
/** *//**
* 计算总页数,静态方法,供外部直接通过类名调用
* @param pageSize 每页记录数
* @param allRow 总记录数
* @return 总页数
*/
public static int countTotalPage(final int pageSize,final int allRow){
int totalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize+1;
return totalPage;
}
/** *//**
* 计算当前页开始记录
* @param pageSize 每页记录数
* @param currentPage 当前第几页
* @return 当前页开始记录号
*/
public static int countOffset(final int pageSize,final int currentPage){
final int offset = pageSize*(currentPage-1);
return offset;
}
/** *//**
* 计算当前页,若为0或者请求的URL中没有”?page=”,则用1代替
* @param page 传入的参数(可能为空,即0,则返回1)
* @return 当前页
*/
public static int countCurrentPage(int page){
final int curPage = (page==0?1:page);
return curPage;
}
}
action实现:
public String coursesPage(){
//课程分页:
pagebean=this.cuService.queryForPage(this.PAGENUMBER, page);
return “suc”;
}
}
jsp页面:
<%@ page language=”java” contentType=”text/html; charset=GB2312″
pageEncoding=”GB2312″%>
<%@ taglib uri=”/struts-tags” prefix=”s”%>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd“>
<html>
<HEAD>
<TITLE>myBody.jsp</TITLE>
<META http-equiv=Content-Type content=”text/html; charset=GB2312″>
<META content=”MSHTML 6.00.5730.13″ name=GENERATOR>
<link href=”css/web1.css” rel=”stylesheet” type=”text/css”>
</HEAD>
<BODY>
<DIV>
<!–背景层–>
<DIV>
<!–top–>
<DIV id=”allMeeting” name=”allMeeting”>
<table cellspacing=0 cellpadding=0
width=”100%” border=0>
<tbody>
<tr>
<td width=”4%”> </td>
<td width=”25%”> 开课时间 </td>
<td width=”10%”> 预计时间 </td>
<td width=”41%”> 课程名称 </td>
<td width=”18%”> 教师 </td>
<td width=”2%”> </td>
</tr>
<s:iterator value=”pagebean.list” id=”ni” status=”act”>
<s:if test=”#act.odd”>
<tr>
</s:if>
<s:else>
<tr>
</s:else>
<td width=”4%”> </td>
<td width=”11%”><s:property value=”#ni.begintime” />
</td>
<td width=”10%”><s:property value=”#ni.awoketype” />
</td>
<td width=”55%”><s:property value=”#ni.csname” />
</td>
<td width=”8%”><s:property value=”#ni.users.uname” />
</td>
<td width=”12%”><a href=”#” onClick=”writeUsrInfoFile();” >加入</a> </td>
<td width=”12%”> </td>
</s:iterator>
共<s:property value=”pagebean.allRow”/> 条记录
共<s:property value=”pagebean.totalPage”/> 页
当前第<s:property value=”pagebean.currentPage”/>页<br/>
<s:if test=”%{pagebean.currentPage == 1}”>
第一页 上一页
</s:if>
<s:else>
<a href=”pagecourses.action?page=1″>第一页</a>
<a href=”pagecourses.action?page=<s:property value=”%{pagebean.currentPage-1}”/>”>上一页</a>
</s:else>
<s:if test=”%{pagebean.currentPage != pagebean.totalPage}”>
<a href=”pagecourses.action?page=<s:property value=”%{pagebean.currentPage+1}”/>”>下一页</a>
<a href=”pagecourses.action?page=<s:property value=”pagebean.totalPage”/>”>最后一页</a>
</s:if>
<s:else>
下一页 最后一页
</s:else>
</table>
</DIV>
</DIV>
</DIV>
<script type=”text/javascript” src=”javascript/language.js”></script>
<script type=”text/javascript” src=”javascript/speed.js”></script>
<script type=”text/javascript” src=”javascript/menu.js”></script>
</BODY>
</HTML>
以上代码是我从工程里抠出来的所有的类和接口都不是完整的,但方法都是可以执行起来的,完整代码请根据需要添加。有疑问可以联系我。
http://ldwloveyou1234.blog.163.com/blog/static/2125843220101122725592