CQ5 - Sling currentPage in model class

aem, java, sling

Solution

you are getting jcr:content/banner because you are calling the resource of a component inside a page, not the page itself.

to get the current page from a component you can use the PageManager:

PageManager pageManager= resource.getResourceResolver().adaptTo(PageManager.class);

Page currentPage = pageManager.getContainingPage(resource);

Problem

In a CQ jsp it's easy to access the currentPage object by simply calling currentPage.getPath(). But I'm trying to remove any scrip lets and separate them out into a model class and can't figure out how to access the currentPage object. I thought I could do something like: ``` public void setResource(Resource resource){ resource.getPath() } ``` But that returns something like: ``` /content/home/subPage/jcr:content/banner ``` I'm just looking to return /content/home/subPage/. I'm trying to use resources to get the path but can't find anything to do this. I know this must be something small that I'm just overlooking. Thanks for the help!

Original source