I would consider using org.springframework.data.domain.Pageable
directly into your controller. This object can then be passed to your JPA layer where it will handle the number of returned results and the size.
The great thing about using Pageable
is that it returns a Page
object which can be used on the front-end to form previous/next page logic.
By default this class uses url parameters 'page' and 'size'; hence page=0&size=10 will return the first 10 items.
Hence in your case the code could look something like:
@ResponseBody
@RequestMapping("/top/pages/")
public List<Post> getAllPosts(@PageableDefault(value=10, page=0) Pageable pageable) throws ServletException {
Page page = postDao.findAll(pageable);
return page.getContent();
}
Notice the annotation @PageableDefault
is just to set up the defaults & it's not required.
In the front-end the next page call can be <a href="/top/pages?page=1">Next</a>
; this will return a list of Posts from 11 to 20.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…