CQ5 - 模型类中的Sling currentPage

Del*_*ung 3 java sling aem

在CQ jsp中,只需调用currentPage.getPath()即可轻松访问currentPage对象.但我正在尝试删除任何脚本,并将它们分离到模型类中,并且无法弄清楚如何访问currentPage对象.我以为我可以这样做:

public void setResource(Resource resource){
    resource.getPath()
}  
Run Code Online (Sandbox Code Playgroud)

但这会返回类似于:

/content/home/subPage/jcr:content/banner
Run Code Online (Sandbox Code Playgroud)

我只是想返回/ content/home/subPage /.我正在尝试使用资源来获取路径,但无法找到任何可以做到这一点.我知道这一定是小事,我只是在俯视.谢谢您的帮助!

san*_*zky 13

你正在获取jcr:content/banner,因为你正在调用页面内部组件的资源,而不是页面本身.

要从组件中获取当前页面,您可以使用PageManager:

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

page currentPage = pageManager.getContainingPage(resource);