简体   繁体   中英

Pagination Without Spring Data

I am creating a web application using Spring MVC and my database isn't supported by Spring Data. How can I add pagination without using Spring Data. My application has a table which list out objects retrieved via a service which has the ability to retrieve paginated results(i am able to pass in the page and result size). The problem comes when the user searches by date range or any other search where parameters are placed in the url. I cannot figure out a way to generate the correct url for the "next" and "prev" links.

If your database isn't supported by Spring Data you can reuse class PageImpl to wrap your result and not use page data in queries, just wrap your search result.

List<T> content = your result set;
Pageable pageable= new PageRequest(int page, int size);
long total = cound found elements;
PageImpl pager = new PageImpl(content,pageable,cound );

in this case, you just reuse class and you don't need to work with calculation. class PageRequest has previous(), next() methods, so you don't need to calculate it.

If you don't want to have spring data as dependency you can just copy and put into your project page classes that you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM