<span style="font-family: Arial, Helvetica, sans-serif;">package accp.hhs.util;</span>
public class PageMoed { private int currentIndex; //当前页 private int rowSize; //行大小 private int sunPage; //总页数 private int beginRow; //起始行 private int startIndex; //显示的页码 private String order; //排序 public String getOrder() { return order; } public void setOrder(String order) { this.order = order; } /** * 起始显示页数 * @return */ public int getStartIndex() { for(int i=1;i<=this.getSunPage();i+=8){ if(this.getCurrentIndex()>=i&&this.getCurrentIndex()<i+9){ startIndex=i; break; } } return startIndex; } /** * 起如行数 * @return */ public int getBeginRow() { //起始行, 当前行数-1*行大小 beginRow=(currentIndex-1)*rowSize; return beginRow; } public void setBeginRow(int beginRow) { this.beginRow = beginRow; } public int getSunPage() { return sunPage; } //总页数 public void setSunPage(int sunRow) { int temp=0; temp=(sunRow%this.getRowSize()==0)?sunRow/this.getRowSize():sunRow/this.getRowSize()+1; System.out.println("总页数: "+temp+" 大小:"+this.getRowSize()); this.sunPage=temp; } /** * 当前页 * @return */ public int getCurrentIndex() { return currentIndex; } public void setCurrentIndex(String currentIndex) { //如是为空=1 if("".equals(currentIndex)||null==currentIndex||!IsNum.isNum(currentIndex)){ currentIndex="1"; } int currentIndexs=Integer.parseInt(currentIndex); //如果当前页大于总页数 if(currentIndexs>=this.getSunPage()){ currentIndexs=this.getSunPage(); } if(currentIndexs<=0){ currentIndexs=1; } this.currentIndex = currentIndexs; } /** * 行数数大小 * @return */ public int getRowSize() { if(rowSize<=0){ rowSize=2; } return rowSize; } public void setRowSize(int rowSize) { this.rowSize = rowSize; } }
</pre><p>JSP 分页技术 原创,测试OK,无论数据库有多少数据,可以尽情的翻!</p><p>因涉及的代码太多!这只是重心代码,其它就是传值,查询数据库之类的!因该不是什么大问题吧。。。。。。。。</p><p>还是把页面代码贴出来吧</p><p></p><pre name="code" class="html"><div> <p> <a href="${pageContext.request.contextPath}/EmpSerive.do?flag=showEmp¤tIndex=${currentIndex-1}"> 上一页</a> 当前页【${currentIndex}/${totalPage}】 <c:forEach var="i" begin="${satrtIndex}" end="${satrtIndex+9}"> <a href="${pageContext.request.contextPath}/EmpSerive.do?flag=showEmp¤tIndex=${i}"> <c:if test="${currentIndex==i}"> <font color='red'> ${i} </font> </c:if> <c:if test="${currentIndex!=i}">${i} </c:if> </a> </c:forEach> <a href="${pageContext.request.contextPath}/EmpSerive.do?flag=showEmp¤tIndex=${currentIndex+1}"> 下一页</a> <input type="text" size="2" id="valuePage"/> <a href="javascript:PageIndex()">跳转</a> </p> <%--页面跳转 --%> <script type="text/javascript"> function PageIndex(){ var pagev=document.getElementById("valuePage"); location.href="${pageContext.request.contextPath}/EmpSerive.do?flag=showEmp¤tIndex="+pagev.value; } </script> </div>
好吧!把实现类的方法也贴出来吧,看不懂不要怪我哈,因为我做测试用了反射机制,让我看我一下子也看不懂了!哈哈,看你需要的就行了。。
/** * 根据hql分页 */ @Override public List<T> getPage(String hql, Object[] paramters, PageMoed pageMeod) { Query query=sessionFactory.getCurrentSession().createQuery(hql); if(paramters!=null){ for(int i=0;i<paramters.length;i++){ query.setParameter(i,paramters[i]); } } query.setFirstResult(pageMeod.getBeginRow()).setMaxResults(pageMeod.getRowSize()); return (List<T>)query.list(); } /** * 根据对象分页 */ @Override public List<T> getPage(T e, PageMoed pageMeod) throws Exception { Class cla=e.getClass(); String tableName=cla.getSimpleName(); Field[] field=cla.getDeclaredFields();//得到所有字段 //SQL语句接接 StringBuffer sb=new StringBuffer(); sb.append(" where 1=1 "); //List<Object> listValue=new ArrayList<Object>(); //?值拼接 for(int i=0;i<field.length;i++){ Field f=field[i]; f.setAccessible(true); Object obj=f.get(e); //得到字段的值 if(obj instanceof Set){ System.out.println("检测到Set....."); ((Set) obj).clear(); }else // 加? 加值 if(!"".equals(obj)&&null!=obj){//如果字段不为空 sb.append(" and "+f.getName()+"= "+obj); //listValue.add(obj); } } //如有有排序的话 if(pageMeod.getOrder()!=null){ sb.append(" "+pageMeod.getOrder()); } String hql="from "+tableName+" "+sb.toString(); System.out.println("HQL: "+hql); Query query=sessionFactory.getCurrentSession().createQuery(hql); //分页 List<T> liste=(List<T>)query.setFirstResult(pageMeod.getBeginRow()).setMaxResults(pageMeod.getRowSize()).list(); //那些年 return liste; }
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
</pre><pre name="code" class="html">