Java code Implementation True paging

Source: Internet
Author: User

In a Javaweb project, paging is a very common and important facet. This time as the record and study, records the page that appears in the project and completes the study record. In this case, the SSH framework is used. The framework can be understood as:

On the JSP page, describe the following code:

1 <DivAlign= "Center">2         <c:ifTest= "${page.currentpage>1}">3             <ahref= "Show_findstessayall.action?currentpage=1" >Home</a>4            <ahref= "Show_findstessayall.action?currentpage=${page.currentpage-1}">Previous page</a>5         </c:if>6         <c:ifTest= "${page.currentpage! = page.totalpage}">7            <ahref= "Show_findstessayall.action?currentpage=${page.currentpage+1}">Next page</a>8            <ahref= "Show_findstessayall.action?currentpage=${page.totalpage}">Last</a>9         </c:if>                        Ten                          One         <formAction= "Show_findstessayall.action"> A Total ${page.totalpage} page -            <inputtype= "text"value= "${page.currentpage}"name= "CurrentPage"size= "1">page -            <inputtype= "Submit"value= "Go"> the         </form>                         -</Div>    

Action Part code:

1  PublicString Findacadcommall () {2         //Page Data Storage3page<acadcomm> page =NewPage<acadcomm>();4         5         //Total Record Count6         intTotalrecord =Showservice.findacadcommrecord (); 7         if(totalrecord!=0){8 Page.settotalrecord (Totalrecord);9             //Total PagesTen             intTotalpage = (totalrecord% page.getpagesize () = = 0)? Totalrecord/page.getpagesize (): Totalrecord/page.getpagesize () +1;  One page.settotalpage (totalpage); A             //Current Page -             intCurrentPage = 1; -String currentpagestring = Req.getparameter ("CurrentPage"); theSystem.out.println ("currentpagestring:" +currentpagestring); -             if(Currentpagestring! =NULL){ -CurrentPage =Integer.parseint (currentpagestring); -             } + page.setcurrentpage (currentpage); -System.out.println ("CurrentPage:" +currentpage); +              AString hql = "from Acadcomm a";//Query Statements at             //the data to display -             if(Totalrecord% page.getpagesize ()!=0 && currentpage==totalpage) { -Page.setdatalist (Showservice.queryforpage (ACADCOMM,HQL, (currentPage-1) *page.getpagesize (), Totalrecord%page.getpagesize ())); -          -}Else { -Page.setdatalist (Showservice.queryforpage (ACADCOMM,HQL, (currentPage-1) *page.getpagesize (), Page.getpagesize ())); in             } -         } toReq.setattribute ("page", page); +          -         return"Findacadcommallsuccess"; the}

Call service

 Public int Findstessayrecord (String hql); // returns the total number of records     Public int Offset,             int length); // returns the current page data

Serviceimpl calls the corresponding function inside the DAO

1 @Override2      Public intFindstessayrecord (String hql) {3         //TODO auto-generated Method Stub4         5         returnStessaydao.getallrowcount (HQL);//Total Record Count6     }7 @Override8      PublicList<stessay> queryforpage (Stessay Stessay, String hql,intOffset,9             intlength) {Ten         //TODO auto-generated Method Stub One         returnstessaydao.queryforpage (hql, offset, length); A}

DAO interfaces are only methods and do not write specific implementations:

1   Public List<stessay> findAll (); 2   Public int Getallrowcount (String hql); 3   Public List<stessay> Queryforpage (finalfinalintfinalint length);

The implementation of the concrete method is given to Daoimpl:

1 /**   2 * Check the number of all records3      * @paramhql Query Criteria4      * @returnTotal Record Count5      */   6 @Override7      Public intGetallrowcount (String hql) {8         return  This. Gethibernatetemplate (). Find (HQL). Size (); 9     }    Ten     /**    One * Paging Query A      * @paramhql Query Criteria -      * @paramoffset start record -      * @paramlength query several records at a time the      * @returnthe collection of records for the query -      */     - @Override -      PublicList<stessay> Queryforpage (FinalString HQL,Final intOffsetFinal intlength) {     +Session session = This. getsession ();  -Query q =session.createquery (HQL);  + Q.setfirstresult (offset);  A q.setmaxresults (length);  atlist<stessay> list =q.list (); -          -System.out.println ("--------paperimpl---------------size ()" +list.size ());  - Session.close ();  -         returnlist;  -}

True paging is how much the page is displayed and how much is loaded from the database, which increases efficiency. False paging is to load all the data inside the database, but only show the parts that need to be displayed, for the data is very many systems, so down, the efficiency will be particularly low.

Java code Implementation True paging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.