Search code examples
javaaem

How to navigate/browse on next/previous page in aem?


I'm developing pages in AEM 6.3. Right now I have a situation to develop a page containing 2 links that browse to next and previous page with respective to current page. Let's say I have 3 pages under my project site /content/project/en.

  • Page A
  • Page B
  • Page C

Let's assume, if I'm on Page B, then my next page should be Page C and previous page should be Page A. Those two links(next/previous) should redirect me to Page A and Page B.

There might be many approaches to do that but what I have in mind is that, I'll be making an ajax get request which will return me next and previous pages in key/value format. To do this I'll be using sling servlet object to get the page path and query builder to make query on that path to get the next and previous page. This approach works fine if there is very limited number of pages but would be time consuming if pages increases.

I want simple solution to implement this. Or is there any plugin in aem to do this? I also tried to do that with currentPage.nextSibling() in sightly but there is no such thing exist in aem.


Solution

  • As addition with new requirement which was page on next/previous link should be sorted in created date with respective of current page. So I came up with following solution.

    I have created a component name 'Next Previous Pagination' and used a WCMUsePojo model in htl language. This model is responsible to get next and previous page of the current page. Following are the jcr queries to get next and previous page.

    For next Page,

    select page.* from [cq:Page] AS page WHERE 
    ISDESCENDANTNODE(page,'/content/project/myCurrentPage') 
    AND page.[jcr:content/jcr:created] >= CAST('${createdDateOfCurrentPage}' AS 
    DATE) AND NAME(page) <>'${currentPageName}' ORDER BY page. 
    [jcr:content/jcr:created] asc Limit 1
    

    For previous page,

    select page.* from [cq:Page] AS page WHERE 
    ISDESCENDANTNODE(page,'/content/project/myCurrentPage') 
    AND page.[jcr:content/jcr:created] <= CAST('${createdDateOfCurrentPage}' AS 
    DATE) AND NAME(page) <>'${currentPageName}' ORDER BY page. 
    [jcr:content/jcr:created] desc Limit 1