Expand my Community achievements bar.

SOLVED

Page object not available

Avatar

Level 5

    Hi All,

        I am invoking a tag lib with slingRequest object in which i have the following code 

import com.day.cq.wcm.api.Page; public class Temp extends SimpleTagSupport{ public static String getValue(SlingHttpServletRequest request){ LOG.info("Resource Path is {}",request.getResource()); Page page   =   request.getResource().adaptTo(Page.class); LOG.info("Page is {}",page); } }

The resource path from the log is  /content/my-cq-page/jcr:content and the page object that i got is null. I am not sure why it is null. Is there a better way to extract a page object from a sling request?

Can someone help me on this?

Thanks,

Jai.

1 Accepted Solution

Avatar

Correct answer by
Level 10
      Please use this: ResourceResolver resourceResolver = request.getResourceResolver(); PageManager pageManager = resourceResolver.adaptTo(PageManager.class); String pageUri = "/content/site/page.html" ; Page page = pageManager.getPage(pageUri);

View solution in original post

3 Replies

Avatar

Employee Advisor

It returns null because the path " /content/my-cq-page/jcr:content" represent cq:PageContent object and not the "cq:Page" object. 

Avatar

Correct answer by
Level 10
      Please use this: ResourceResolver resourceResolver = request.getResourceResolver(); PageManager pageManager = resourceResolver.adaptTo(PageManager.class); String pageUri = "/content/site/page.html" ; Page page = pageManager.getPage(pageUri);

Avatar

Level 5

It is working!!!

Managed to accomplish it like this 

ResourceResolver resourceResolver = request.getResourceResolver(); Resource resource = request.getResource(); PageManager pageManager = resourceResolver.adaptTo(PageManager.class); Page page = pageManager.getContainingPage(resource);

Thanks kunal and edubey for your reply.