SSH实现分页的两个例子.doc_第1页
SSH实现分页的两个例子.doc_第2页
SSH实现分页的两个例子.doc_第3页
SSH实现分页的两个例子.doc_第4页
SSH实现分页的两个例子.doc_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

先是一个page的bean:package com.leatherstore.other; public class Page /* 是否有上一页 */ private boolean hasPrePage; /* 是否有下一页 */ private boolean hasNextPage; /* 每页的数量 */ private int everyPage; /* 总页数 */ private int totalPage; /* 当前页*/ private int currentPage; /* 起始点 */ private int beginIndex; /* 总记录数*/ private int totalCount; /* * return totalCount */ public int getTotalCount() return totalCount; /* * param totalCount 要设置的 totalCount */ public void setTotalCount(int totalCount) this.totalCount = totalCount; /* The default constructor */ public Page() /* construct the page by everyPage * param everyPage * */ public Page(int everyPage) this.everyPage = everyPage; /* The whole constructor */ public Page(boolean hasPrePage, boolean hasNextPage, int everyPage, int totalPage, int currentPage, int beginIndex,int totalCount) this.hasPrePage = hasPrePage; this.hasNextPage = hasNextPage; this.everyPage = everyPage; this.totalPage = totalPage; this.currentPage = currentPage; this.beginIndex = beginIndex; this.totalCount = totalCount; /* * return * Returns the beginIndex. */ public int getBeginIndex() return beginIndex; /* * param beginIndex * The beginIndex to set. */ public void setBeginIndex(int beginIndex) this.beginIndex = beginIndex; /* * return * Returns the currentPage. */ public int getCurrentPage() return currentPage; /* * param currentPage * The currentPage to set. */ public void setCurrentPage(int currentPage) this.currentPage = currentPage; /* * return * Returns the everyPage. */ public int getEveryPage() return everyPage; /* * param everyPage * The everyPage to set. */ public void setEveryPage(int everyPage) this.everyPage = everyPage; /* * return * Returns the hasNextPage. */ public boolean getHasNextPage() return hasNextPage; /* * param hasNextPage * The hasNextPage to set. */ public void setHasNextPage(boolean hasNextPage) this.hasNextPage = hasNextPage; /* * return * Returns the hasPrePage. */ public boolean getHasPrePage() return hasPrePage; /* * param hasPrePage * The hasPrePage to set. */ public void setHasPrePage(boolean hasPrePage) this.hasPrePage = hasPrePage; /* * return Returns the totalPage. * */ public int getTotalPage() return totalPage; /* * param totalPage * The totalPage to set. */ public void setTotalPage(int totalPage) this.totalPage = totalPage; 然后构建一个page的工厂PageUtil: package com.leatherstore.other; public class PageUtil /* * Use the origin page to create a new page * * param page * param totalRecords * return */ public static Page createPage(Page page, int totalRecords) return createPage(page.getEveryPage(), page.getCurrentPage(), totalRecords); /* * the basic page utils not including exception handler * * param everyPage * param currentPage * param totalRecords * return page */ public static Page createPage(int everyPage, int currentPage, int totalRecords) everyPage = getEveryPage(everyPage); currentPage = getCurrentPage(currentPage); int beginIndex = getBeginIndex(everyPage, currentPage); int totalPage = getTotalPage(everyPage, totalRecords); boolean hasNextPage = hasNextPage(currentPage, totalPage); boolean hasPrePage = hasPrePage(currentPage); return new Page(hasPrePage, hasNextPage, everyPage, totalPage, currentPage, beginIndex, totalRecords); private static int getEveryPage(int everyPage) return everyPage = 0 ? 10 : everyPage; private static int getCurrentPage(int currentPage) return currentPage = 0 ? 1 : currentPage; private static int getBeginIndex(int everyPage, int currentPage) return (currentPage - 1) * everyPage; private static int getTotalPage(int everyPage, int totalRecords) int totalPage = 0; if (totalRecords % everyPage = 0) totalPage = totalRecords / everyPage; else totalPage = totalRecords / everyPage + 1; return totalPage; private static boolean hasPrePage(int currentPage) return currentPage = 1 ? false : true; private static boolean hasNextPage(int currentPage, int totalPage) return currentPage = totalPage | totalPage = 0 ? false : true; 然后建一个专用的bean: package com.leatherstore.hibernate.domain; import java.util.List; import com.leatherstore.other.Page; public class Result private Page page; /分页信息 private List content; /每页显示的集合 /* * The default constructor */ public Result() super(); /* * The constructor using fields * * param page * param content */ public Result(Page page, List content) this.page = page; this.content = content; /* * return Returns the content. */ public List getContent() return content; /* * return Returns the page. */ public Page getPage() return page; /* * param content * The content to set. */ public void setContent(List content) this.content = content; /* * param page * The page to set. */ public void setPage(Page page) this.page = page; 然后在数据访问层写两个方法: ProductDAO: public List getProductByPage(Page page); public int getProductCount(); /返回数据的总数 ProductDAO的接口实现ProductDAOImpl: /为了在spring里统一编程风格:我用的回调的方法 public List getProductByPage(final Page page) return this.getHibernateTemplate().executeFind(new HibernateCallback() public Object doInHibernate(Session session) throws HibernateException, SQLException Query query=session.createQuery(from Productinfo); query.setFirstResult(page.getBeginIndex(); /hibernate分页的精髓 呵呵 query.setMaxResults(page.getEveryPage(); return query.list(); ); public int getProductCount() List list=this.getHibernateTemplate().find(select count(*) from Productinfo); return (Integer)list.iterator().next().intValue(); 然后是业务层: IProduct: public Result listProduct(Page page); 然后IProduct接口的实现:IProductImpl: private ProductDAO productDAO; public void setProductDAO(ProductDAO productDAO) ductDAO=productDAO; public Result listProduct(Page page) int totalRecords = ductDAO.getProductCount(); page = PageUtil.createPage(page, totalRecords); List products = ductDAO.getProductByPage(page); return new Result(page, products); 然后再代理层: ProductProxy: IProduct pro=(IProduct)AppContext.getInstance().getappcontext().getBean(productServicewithTran); public Result productlist(Page page) try return pro.listProduct(page); catch(DataAccessException ex) ex.printStackTrace(); return null; 呵呵 终于到productAction啦 显示前方法的代码 Page page = new Page(); /实例化一个page对象 page.setEveryPage(10); /设置每页显示的条数 page.setCurrentPage(1); /为第一页 Result result = ductlist(page); request.setAttribute(page, pageinfo); request.setAttribute(productlist, list); return mapping.findForward(showProduct); 接着就是jsp页面了 /中间迭代所要显示的数据   首页 上一页 首页 上一页 下一页 尾页 下一页 尾页 页次$page.currentPage /$page.totalPage    共$page.totalCount 条记录 可以显示相应的页面信息 然后productAction里面的showProductByTag代码如下: Page page = new Page(); page.setEveryPage(10); String pagemark = request.getParameter(goto); if (pagemark = null) String state = request.getParameter(index); String pageno = request.getParameter(pageno); System.out.println(pageno= + pageno); if (first.equals(state) page.setCurrentPage(1); Result result = ductlist(page); request.setAttribute(page, result.getPage(); request.setAttribute(productlist, result.getContent(); else if (prew.equals(state) page.setCurrentPage(Integer.parseInt(pageno); Result result = ductlist(page); request.setAttribute(page, result.getPage(); request.setAttribute(productlist, result.getContent(); else if (next.equals(state) page.setCurrentPage(Integer.parseInt(pageno); Result result = ductlist(page); request.setAttribute(page, result.getPage(); request.setAttribute(productlist, result.getContent(); else if (end.equals(state) page.setCurrentPage(Integer.parseInt(pageno); Result result = ductlist(page); request.setAttribute(page, result.getPage(); request.setAttribute(productlist, result.getContent(); else page.setCurrentPage(Integer.parseInt(pagemark); Result result = ductlist(page); request.setAttribute(page, result.getPage(); request.setAttribute(productlist, result.getContent(); return mapping.findForward(showProduct);- SSH的分页网上有不少的例子,有利用session的,有利用分页组件的。我几个师兄原来搞的SSH项目也有一个成熟的分页插件。具体业务实现类中的分页方法:public List get*(int pageNO) DetachedCriteria dc = DetachedCriteria.forClass(*.class); List list=*Dao.getList(dc,pageNO,15); int a = 0; if (list.equals(null)a=1; return list; public PageBean getBean(int pageNO) * jg; DetachedCriteria dc = DetachedCriteria.forClass(*.class); PageBean pb=collegeDao.getPageBean(dc,pageNO,15); return pb; 然后是一个PageBean的工具类,负责创建分页属性和基本逻辑。 然后是页面的bean获取输出信息及分页属性。 我觉得单纯的拷贝,自己用的不是很顺手。于是自己也搞了一个,个人认为可以清晰的分层,实现这个分页。分层还是传统的SSH七层结构。 SSH结构思想参考我的另一篇随笔SSH思想之我见! 下面是分页思想,一个初始化方法和一个分页实现方法: 分页的util类: package com.sy.util; import java.util.List; SuppressWarnings(unchecked) public class Page private int pageSize; private int totalPage; private int rowCount; private int currentPage; private int prePage; private int nextPage; private boolean hasNextPage; private boolean hasPreviousPage; private List list; public Page() this.pageSize=10; public int getPageSize() return pageSize; public void setPageSize(int pageSize) this.pageSize = pageSize; public int getTotalPage() return totalPage; public void setTotalPage(int totalPage) this.totalPage = totalPage; public int getRowCount() return rowCount; public void setRowCount(int rowCount) this.rowCount = rowCount; public int getCurrentPage() return currentPage; public void setCurrentPage(int currentPage) this.currentPage = currentPage; public int getPrePage() return prePage; public void setPrePage(int prePage) this.prePage = prePage; public int getNextPage() return nextPage; public void setNextPage(int nextPage) this.nextPage = nextPage; public boolean isHasNextPage() return hasNextPage; public void setHasNextPage(boolean hasNextPage) this.hasNextPage = hasNextPage; public boolean isHasPreviousPage() return hasPreviousPage; public void setHasPreviousPage(boolean hasPreviousPage) this.hasPreviousPage = hasPreviousPage; public List getList() return list; public void setList(List list) this.list = list; 分页的数据库操作和逻辑判断我把他单独用一个PageDaoImpl来实现: /* * 施杨的分页daoimpl类 * */ package com.sy.dao.impl; import java.sql.SQLException; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.sy.dao.BaseDao; import com.sy.util.Page; SuppressWarnings(unchecked) public class PageDaoImpl extends HibernateDaoSupport private String hql; public Page page; public int start; public BaseDao dao; public void setDao(BaseDao dao) this.dao = dao; public void init(int start,String tableName) / init page = new Page(); this.hql = from +tableName; this.start = start; setRowCount(); setTotalPage(); setCurrentPage(); setPrePage(); setNextPage(); setPreOrNextBoolean(); public int getRowCount() List list = dao.find(hql); if(list.isEmpty() return 0; return list.size(); public Page getPage() List list = (List)getHibernateTemplate().execute(new HibernateCallback() public Object doInHibernate(Session session) throws HibernateException, SQLException Query query = session.createQuery(hql); query.setFirstResult(getStartIndex(); query.setMaxResults(page.getPageSize(); return query.list(); ); page.setList(list); return page; public void setPreOrNextBoolean() if (page.getCurrentPage() = page.getTotalPage() page.setHasNextPage(false); else page.setHasNextPage(true); public void setCurrentPage() if (start page.getTotalPage() page.setCurrentPage(page.getTotalPage(); page.setCurrentPage(start); public void setPrePage() page.setPrePage(page.getCurrentPage() - 1); public void setNextPage() page.setNextPage(page.getCurrentPage() + 1); public void setTotalPage() int rowCount = getRowCount(); int pageSize = page.getPageSize(); if (rowCount pageSize) if (rowCount % pageSize = 0) page.setTotalPage(rowCount / pageSize); else page.setTotalPage(1 + (rowCount / pageSize); else page.setTotalPage(1); public void setRowCount() page.setRowCount(getRowCount(); public int getStartIndex() int startIndex = 0; if (start page.getTotalPage() startIndex = page.getPageSize() * (page.getTotalPage() - 1); else startIndex = page.getPageSize() * (start - 1); return startIndex; 然后是业务层接口,举例AdminService.java /管理员service层接口 package com.sy.service; import java.util.List; import com.sy.util.Page; import com.sy.vo.Admin; public interface AdminService extends BaseService /分页初始化 public void init(int pno); /分页实现 public Page getPage(); 实现类AdminServiceImpl.java /管理员service层实现类 package com.sy.service.impl; import java.util.List; import c

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论