Search code examples
javaaemsling

How to access the properties of a component from another component which lies on same page? AEM Java Sling


Assume We have component A and Component B on Home page of AEM instance. I want Component A properties value on Component B in sling java class.

Can you please provide some code to explain your answer. It would be easy for me to understand


Solution

  • @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");
                }
            }
        }
    }
    

    Assigned Component A property(headline) value to Component B property(title)