ssh分页实例
来源:互联网 发布:c 仿ce源码 编辑:程序博客网 时间:2024/05/16 12:52
ssh分页实例
先是一个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;
- }
- }
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;
- }
- }
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;
- }
- }
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();
- }
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: public Result listProduct(Page page);然后IProduct接口的实现:IProductImpl:
- private ProductDAO productDAO;
- public void setProductDAO(ProductDAO productDAO){
- this.productDAO=productDAO;
- }
- public Result listProduct(Page page) {
- int totalRecords = this.productDAO.getProductCount();
- page = PageUtil.createPage(page, totalRecords);
- List products = this.productDAO.getProductByPage(page);
- return new Result(page, products);
- }
private ProductDAO productDAO; public void setProductDAO(ProductDAO productDAO){ this.productDAO=productDAO; } public Result listProduct(Page page) { int totalRecords = this.productDAO.getProductCount(); page = PageUtil.createPage(page, totalRecords); List products = this.productDAO.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;
- }
- }
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 = pdp.productlist(page);
- request.setAttribute("page", pageinfo);
- request.setAttribute("productlist", list);
- return mapping.findForward("showProduct");
Page page = new Page(); //实例化一个page对象 page.setEveryPage(10); //设置每页显示的条数 page.setCurrentPage(1); //为第一页 Result result = pdp.productlist(page); request.setAttribute("page", pageinfo); request.setAttribute("productlist", list); return mapping.findForward("showProduct");接着就是jsp页面了
- <logic:iterate id="product" name="productlist">
- //中间迭代所要显示的数据
- </logic:iterate>
- <tr>
- <td width="80" height="30"> </td>
- <logic:equal value="true" name="page" property="hasPrePage">
- <td width="150" height="30"><div align="right"><a href="../product.do?method=showProductByTag&index=first&msg=${msg }">首页</a></div></td>
- <td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=prew&pageno=${page.currentPage -1}&msg=${msg }">上一页</a></div></td>
- </logic:equal>
- <logic:notEqual value="true" name="page" property="hasPrePage">
- <td width="150" height="30"><div align="right">首页</div></td>
- <td width="80" height="30"><div align="center">上一页</div></td>
- </logic:notEqual>
- <logic:equal value="true" name="page" property="hasNextPage">
- <td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=next&pageno=${page.currentPage +1 }&msg=${msg }">下一页</a></div></td>
- <td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=end&pageno=${page.totalPage }&msg=${msg }">尾页</a></div></td>
- </logic:equal>
- <logic:notEqual value="true" name="page" property="hasNextPage">
- <td width="80" height="30"><div align="center">下一页</div></td>
- <td width="80" height="30"><div align="center">尾页</div></td>
- </logic:notEqual>
- <td height="30" colspan="3"><div align="center">页次${page.currentPage }/${page.totalPage } 共${page.totalCount }条记录</div> <div align="center"></div></td>
- </tr>
<logic:iterate id="product" name="productlist"> //中间迭代所要显示的数据 </logic:iterate> <tr> <td width="80" height="30"> </td> <logic:equal value="true" name="page" property="hasPrePage"> <td width="150" height="30"><div align="right"><a href="../product.do?method=showProductByTag&index=first&msg=${msg }">首页</a></div></td> <td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=prew&pageno=${page.currentPage -1}&msg=${msg }">上一页</a></div></td> </logic:equal> <logic:notEqual value="true" name="page" property="hasPrePage"> <td width="150" height="30"><div align="right">首页</div></td> <td width="80" height="30"><div align="center">上一页</div></td> </logic:notEqual> <logic:equal value="true" name="page" property="hasNextPage"> <td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=next&pageno=${page.currentPage +1 }&msg=${msg }">下一页</a></div></td> <td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=end&pageno=${page.totalPage }&msg=${msg }">尾页</a></div></td> </logic:equal> <logic:notEqual value="true" name="page" property="hasNextPage"> <td width="80" height="30"><div align="center">下一页</div></td> <td width="80" height="30"><div align="center">尾页</div></td> </logic:notEqual> <td height="30" colspan="3"><div align="center">页次${page.currentPage }/${page.totalPage } 共${page.totalCount }条记录</div> <div align="center"></div></td> </tr>可以显示相应的页面信息
然后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 = pdp.productlist(page);
- request.setAttribute("page", result.getPage());
- request.setAttribute("productlist", result.getContent());
- } else if ("prew".equals(state)) {
- page.setCurrentPage(Integer.parseInt(pageno));
- Result result = pdp.productlist(page);
- request.setAttribute("page", result.getPage());
- request.setAttribute("productlist", result.getContent());
- } else if ("next".equals(state)) {
- page.setCurrentPage(Integer.parseInt(pageno));
- Result result = pdp.productlist(page);
- request.setAttribute("page", result.getPage());
- request.setAttribute("productlist", result.getContent());
- } else if ("end".equals(state)) {
- page.setCurrentPage(Integer.parseInt(pageno));
- Result result = pdp.productlist(page);
- request.setAttribute("page", result.getPage());
- request.setAttribute("productlist", result.getContent());
- }
- } else {
- page.setCurrentPage(Integer.parseInt(pagemark));
- Result result = pdp.productlist(page);
- request.setAttribute("page", result.getPage());
- request.setAttribute("productlist", result.getContent());
- }
- return mapping.findForward("showProduct");
- ssh分页实例
- ssh分页实例
- ssh分页实例
- 实例:SSh结合Easyui实现Datagrid的分页显示
- 实例:SSh结合Easyui实现Datagrid的分页显示
- 实例:SSh结合Easyui实现Datagrid的分页显示
- 实例:SSh结合Easyui实现Datagrid的分页显示
- SSH分页
- ssh分页
- SSH分页
- SSH分页
- SSH分页
- SSH分页
- SSH分页
- ssh分页
- SSH分页
- ssh分页
- SSH分页
- VS 2005 Add-in开发随笔
- 使用拆分器控件splitter时窗体布局
- http_load web服务器压力测试工具
- JS实现的随机点名的模型
- PHP对象递归引用造成内存泄漏
- ssh分页实例
- 如何正确理解自动化测试?
- Lucene2.0中最常用的基本操作
- 免费jsp空间建站攻略【eatj】【入门】【教程】
- 网站压力测试工具集
- 做事原则
- linux 内核编译说明
- SQL操作全集
- 一个全新的开始