【问题标题】:How to access the properties of a component from another component which lies on same page? AEM Java Sling如何从位于同一页面上的另一个组件访问组件的属性? AEM Java 吊索
【发布时间】:2022-01-19 20:13:15
【问题描述】:

假设我们在 AEM 实例的主页上有组件 A 和组件 B。 我想要 sling java 类中组件 B 上的组件 A 属性值。

您能否提供一些代码来解释您的答案。对我来说很容易理解

【问题讨论】:

  • 这确实取决于,但您可以在组件 A 中注入 currentPage,然后从组件 B 获取内容资源(如果它是固定路径)并获取道具(getValueMap)或使资源适应您的吊索模型。如果不是固定路径则可以在页面内“搜索”组件B的资源类型

标签: java aem sling


【解决方案1】:
@PostConstruct
protected void init() {
    // ArrayList<Resource> childList = new ArrayList<Resource>();
    Resource childResource = resource.getParent();
    if (childResource != null) {
        Iterator<Resource> children = childResource.listChildren();
        while (children.hasNext()) {
            Resource child = children.next();
            String parentNodeName = child.getName();
            if (parentNodeName.equals("component-a")) {
                // childList.add(child); 
                title = child.getValueMap().get("headline", "headline value");
            }
        }
    }
}

将组件 A 属性(headline) 值分配给组件 B 属性(title)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多