二级考试-浅谈JSP下的Hibernate分页技术.docx

上传人:hs****ma 文档编号:563018685 上传时间:2024-02-13 格式:DOCX 页数:7 大小:14.45KB
返回 下载 相关 举报
二级考试-浅谈JSP下的Hibernate分页技术.docx_第1页
第1页 / 共7页
二级考试-浅谈JSP下的Hibernate分页技术.docx_第2页
第2页 / 共7页
二级考试-浅谈JSP下的Hibernate分页技术.docx_第3页
第3页 / 共7页
二级考试-浅谈JSP下的Hibernate分页技术.docx_第4页
第4页 / 共7页
二级考试-浅谈JSP下的Hibernate分页技术.docx_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《二级考试-浅谈JSP下的Hibernate分页技术.docx》由会员分享,可在线阅读,更多相关《二级考试-浅谈JSP下的Hibernate分页技术.docx(7页珍藏版)》请在金锄头文库上搜索。

1、 二级考试:浅谈JSP下的Hibernate分页技术1. 从数据库中取得记录,在内存中再划分。但假如遇到记录数很大的时候效率很成问题。2. 采纳Hibernate的物理分页,每次只是取一页。从客户端传进来的是第几页和每页多少条记录,要首先查询符合记录的总记录数,再依据总记录数和当前页,每页记录数可以算出要取的是数据库中的第几条记录。但2次查询不行避开了。所以总结了两种方式的优劣,假如数据量不是特别大的话(百万以上),采纳第一种方法,否则可选择其次种。由于我要操作的数据库信息量没有到达大的标准,所以我采纳了第一种方法,下面细说。首先看一下我的一个action:public ActionForwa

2、rd queryZcDoc(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)IZcDocService zcDocService=(IZcDocService)Application.getInstance().getBean(“zcDocServiceProxy“);List docList=zcDocService.queryZcDoc();request.setAttribute(“doc“, subMessList);return mappin

3、g.findForward(“queryDoc“);很简洁的代码,就是查询数据,扔到一个List里面,然后setAttribute,再在jsp页面显示就可以了。接下来谈分页,考虑到了简洁性和通用性,我把分页的代码单独封装到了一个类里面去,下面看看这个类:public class Fenye public List fenye(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)List list=(ArrayList) request.getAttribu

4、te(“list“);/*这里有人可能就看不懂了,为什么要带这些参数?由于我上面的action方法是分页之前的方法,所以不能看出来。下面贴一下用到分页之后的action方法:public ActionForward queryZcDoc(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) IZcDocService zcDocService=(IZcDocService)Application.getInstance().getBean(“zcDocSe

5、rviceProxy“);List docList=zcDocService.queryZcDoc();request.setAttribute(“list“, docList);List subMessList=new Fenye().fenye(mapping, form, request, response);request.setAttribute(“doc“, subMessList);return mapping.findForward(“queryDoc“);和上面的一比拟,其实就多了两行代码,为的就是保持页面的简洁性而使用调用的方法,然后再将需要的数据返回。那接着往下看:*/L

6、ist subMessList=null;/这个到时候存的是用分页技术之后的要显示的记录int showCount =5; /每页显示的记录数。int showPage = 1; /当前显示页码数。int size =list.size(); /所取得的数据的总条数。int pageCount = (size-1)/showCount + 1;/需要显示的总页数if(size到了这里,java代码就写完了,不多吧加括号一共33行。接下来就要到jsp里面去显示了。也是为了页面的干净和通用性,我把分页显示的东东放到了一个jsp里面。下面看这个jsp:% page language=“java“ p

7、ageEncoding=“gb18030“%div align=centerbr%String method=request.getParameter(“method“);method这个参数呢,是要区分对待详细那个action的那个方法String action=request.getParameter(“action“);action这个参数的作用,看下面就知道了int showPage = (Integer)(request.getAttribute(“showPage“).intValue();int size = (Integer)(request.getAttribute(“siz

8、e“).intValue();int pageCount = (Integer)(request.getAttribute(“pageCount“).intValue();int page1=showPage-1;int page2=showPage+1;int LastPage=pageCount;%out.println(“总共有“+size+“条记录 “);out.println(“总共有“+pageCount+“页 “);out.println(“当前是第“+showPage+“页 “);if(showPage 1)out.println(“a href=“+action+“.do?m

9、ethod=“+method+“elseout.println(“第一页“);%if(showPage 1)out.println(“a href=“+action+“.do?method=“+method+“elseout.println(“上一页“);%if(showPage pageCount)out.println(“a href=“+action+“.do?method=“+method+“elseout.println(“下一页“);%if(showPagepageCount)out.println(“a href=“+action+“.do?method=“+method+“elseout.println(“尾页“);%/div关于这个jsp的代码,不用解释太多了吧。再有就是详细的显示页面中,用jsp:include page=“./fenye.jsp?action=link“/jsp:include语句将它包含到相应为止就可以了。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号