You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.getPath won't work because getPath() is a method on the Page class, not a direct property.
  • PageManager isn't directly available in the default context: Your code references pageManager directly, 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.pagePath is empty or malformed, calling pageManager.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:

  1. Check for valid page path first: We first verify properties.pagePath exists to avoid passing an empty value to the PageManager.
  2. Retrieve PageManager correctly: Using ${sling.getService(com.day.cq.wcm.api.PageManager)} gets us a valid PageManager instance from the Sling context.
  3. Call methods with parentheses: thatPage.getPath() correctly invokes the method to get the page's path.
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 02:27:28