在 CQ jsp 中,只需调用 currentPage.getPath() 即可轻松访问 currentPage 对象。但我正在尝试删除任何 scrip lets 并将它们分离到模型类中,但无法弄清楚如何访问 currentPage 对象。我以为我可以做类似的事情:
public void setResource(Resource resource){
resource.getPath()
}
但这会返回类似的东西:
/content/home/subPage/jcr:content/banner
我只是想返回 /content/home/subPage/。我正在尝试使用资源来获取路径,但找不到任何方法来执行此操作。我知道这一定是我忽略的小东西。感谢您的帮助!
最佳答案
您获得 jcr:content/banner 是因为您调用的是页面内组件的资源,而不是页面本身。
要从组件中获取当前页面,您可以使用 PageManager:
PageManager pageManager= resource.getResourceResolver().adaptTo(PageManager.class);
Page currentPage = pageManager.getContainingPage(resource);
关于java - CQ5 - 模型类中的 Sling currentPage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17415951/