List Child pages with Sling model


 List Child pages with Sling model:


Step1: Create a Component with mandatory fields(cq:dialog & component.html)





 

I Created a component with name pageinfo with in that create cq:dialog and rename the .jsp file to .html


Step2: Create a Interface class, Here we created with name PageInfo.java


package com.myproject.aem.core.models;


import com.day.cq.wcm.api.Page;


import java.util.Iterator;

import java.util.List;


public interface PageInfo {

    public List<Page> getPageS();

    public Iterator<Page> getPageChild();

}


Step3:Create Implementation class, Here we created  with name PageInfoImpl.java


package com.myproject.aem.core.models;

import com.day.cq.wcm.api.Page;

import com.day.cq.wcm.api.PageManager;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.DefaultInjectionStrategy;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import javax.inject.Inject;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;


@Model(adaptables = SlingHttpServletRequest.class, adapters = PageInfo.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

public class PageInfoImpl implements PageInfo{


    private static final Logger LOG = LoggerFactory.getLogger(PageInfoImpl.class);


    @ScriptVariable

    PageManager pageManager;


    @Inject

    Resource resource;



    @Override

    public List<Page> getPageS() {

        List<Page> childPage=new ArrayList<>();

        Page home=pageManager.getPage("/content/myproject/us/en");

        Iterator<Page> childs= home.listChildren();

        while (childs.hasNext()){

            Page page= childs.next();

            childPage.add(page);

        }

        return childPage;

    }


}




Now build the code through IntelliJ or eclipse any other Ide..


Step4: Write Slightly code on html, Here we writing slightly on Paging.html


<sly data-sly-use.child="com.myproject.aem.core.models.PageInfo"> </sly> // here PageInfo is the Interface class Name

<div data-sly-list.item="${child.pageS}">

<p><b> ${item.title}  :  ${item.name} </b></p>

</div>



Now add the component in page and refresh the page you will get all  the child pages (PageTitle: PageName)


Thank to AEM GEEKS,
All the material taken from AEM Geeks, if you have any doubts please refer the bellow link:

https://www.youtube.com/watch?v=Bzt0CJTgiR0

Comments

Popular posts from this blog

Data-sly-list & Data-sly-repeat

Java HashMap

Java Packages