Sightly通过路径获取Page对象:组件代码失效问题咨询
Troubleshooting Your AEM HTL Component Code
Let's walk through what's going wrong with your HTL code and how to fix it step by step:
Key Issues in Your Current Code
- Missing parentheses for method calls: In HTL, you need to include parentheses when calling Java methods.
thatPage.getPathwon't work becausegetPath()is a method on thePageclass, not a direct property. - PageManager isn't directly available in the default context: Your code references
pageManagerdirectly, but this object isn't automatically exposed in the standard HTL context. You need to retrieve it via the Sling API first. - No check for empty/invalid page path: If
properties.pagePathis empty or malformed, callingpageManager.getPage()will return null, which could lead to unexpected behavior.
Fixed HTL Code (Direct Approach)
Here's the corrected version that addresses all the above issues:
<sly data-sly-test.pagePath="${properties.pagePath}" data-sly-test.pageManager="${sling.getService(com.day.cq.wcm.api.PageManager)}" data-sly-test.thatPage="${pageManager.getPage(pagePath)}" data-sly-unwrap> <div>${thatPage.getPath()}</div> </sly>
Breakdown of the Fixes:
- Check for valid page path first: We first verify
properties.pagePathexists to avoid passing an empty value to the PageManager. - Retrieve PageManager correctly: Using
${sling.getService(com.day.cq.wcm.api.PageManager)}gets us a valid PageManager instance from the Sling context. - Call methods with parentheses:
thatPage.getPath()correctly invokes the method to get the page's path. - Simplified method parameter: We don't need the
@path=syntax here—HTL lets you pass variables directly as method arguments for single-parameter methods.
Better Practice: Use a Sling Model
For cleaner, more maintainable code (and to follow AEM best practices), move the page retrieval logic to a Sling Model instead of handling it directly in HTL.
Example Sling Model:
import com.day.cq.wcm.api.Page; import com.day.cq.wcm.api.PageManager; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.SlingObject; import org.apache.sling.models.annotations.injectorspecific.ValueMapValue; @Model(adaptables = Resource.class) public class PageReferenceModel { @ValueMapValue private String pagePath; @SlingObject private PageManager pageManager; public Page getTargetPage() { // Guard clause for empty path if (pagePath == null || pagePath.isBlank()) { return null; } return pageManager.getPage(pagePath); } public String getTargetPagePath() { Page targetPage = getTargetPage(); return targetPage != null ? targetPage.getPath() : null; } }
Updated HTL with Sling Model:
<sly data-sly-use.model="com.yourproject.models.PageReferenceModel" data-sly-test.targetPage="${model.targetPage}" data-sly-unwrap> <div>${model.targetPagePath}</div> </sly>
This approach separates your business logic from the view layer, making it easier to test and update later.
内容的提问来源于stack exchange,提问作者Dileepa
相关产品推荐
相关产品推荐

