JSP 分页技术 原创,测试OK,无论数据库有多少数据,可以尽情的翻!

来源:互联网 发布:怎么使用淘宝客买东西 编辑:程序博客网 时间:2024/05/01 15:20
<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) {//如是为空=1if("".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分页     */@Overridepublic 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();}/** * 根据对象分页 */@Overridepublic 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">


原创粉丝点击