Expand my Community achievements bar.

SOLVED

Show the level1 child pages in my sightly

Avatar

Level 2

Hi All,

I am using AEM 6.5 and using the below code for showing only level1 child page from the path /content/testing/en/india.

But the issue is the it is showing all the child pages means level 1 , level 2 and level 3 etc. under /content/testing/en/india.

 

Below are my code.

 

********************************** JAVA ******************************************

import java.util.Iterator;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageFilter;
import com.day.cq.wcm.api.PageManager;
import com.te.aem.sling.model.IndustryModel;

@Model(adaptables = SlingHttpServletRequest.class,
adapters = IndustryModel.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class IndustryModelImpl implements IndustryModel {

private static final Logger logger = LoggerFactory.getLogger(IndustryModelImpl.class);

Page childPage;

@inject
@Via("resource")
private String linkLabel;

@inject
@Via("resource")
private String linkPath;


@SlingObject
ResourceResolver resourceResolver;

@Deleted Account
SlingHttpServletRequest request;

@Override
public String getLinkLabel() {
return linkLabel;
}
@Override
public String getLinkPath() {


//childTitle=linkPath;
return linkPath;

}

@PostConstruct
protected void init() {

logger.info("linkLabel ====== " + linkLabel);
logger.info("linkPath ============ " + linkPath);
//String children = linkPath;
ResourceResolver resourceResolver = request.getResourceResolver();
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
//ArrayList pageList = new ArrayList();
Page rootPage = pageManager.getPage(linkPath);
logger.info("rootPage === ++++++++++++ " + rootPage);
//Page rootPage = linkPath;
Iterator<Page> rootPageIterator = rootPage.listChildren(null, false);
//Iterator<Page> rootPageIterator = rootPage.listChildren(new PageFilter(), true);
//Iterator<Page> rootPageIterator = rootPage.listChildren();
while(rootPageIterator.hasNext())
{

childPage = rootPageIterator.next();
String childTitle = childPage.getTitle();
logger.info("childTitle === "+childTitle);
//pageList.add(path);
}

}


}

 

*************************************** SIGHTLY **************************************

<sly data-sly-use.industryModel="com.test.aem.sling.model.IndustryModel">

<h2>Haritha test :${industryModel.linkPath}</h2>

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @haritha6 ,

Please update your code with the below line, It will give results only for Level -1 Pages.

Iterator<Page> rootPageIterator = rootPage.listChildren(new PageFilter(false, false), false);

Here, PageFilter have two parameter as PageFilter(boolean includeInvalid, boolean includeHidden) and listChilder have two parameter as listChildren(Filter<Page> filter, boolean deep).

Hope this could help you !!!

Regards

Shiv

Shiv Prakash

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @haritha6 ,

Please update your code with the below line, It will give results only for Level -1 Pages.

Iterator<Page> rootPageIterator = rootPage.listChildren(new PageFilter(false, false), false);

Here, PageFilter have two parameter as PageFilter(boolean includeInvalid, boolean includeHidden) and listChilder have two parameter as listChildren(Filter<Page> filter, boolean deep).

Hope this could help you !!!

Regards

Shiv

Shiv Prakash

Avatar

Level 2

Hi Prakash,

Now my level 1 child pages are coming fine in my java code as shown below.

 

But I want to show this level1 child title  in my sightly like below and those are not coming in my page.

 

********************** Sightly ***************************

<sly data-sly-use.industryModel="com.te.aem.sling.model.IndustryModel">

<h2>Haritha test :${industryModel.linkPath}</h2>

<li>
${industryModel.childTitle}
</li>

 

********************************** JAVA ******************************************

import java.util.Iterator;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageFilter;
import com.day.cq.wcm.api.PageManager;
import com.te.aem.sling.model.IndustryModel;

@Model(adaptables = SlingHttpServletRequest.class,
adapters = IndustryModel.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class IndustryModelImpl implements IndustryModel {

private static final Logger logger = LoggerFactory.getLogger(IndustryModelImpl.class);

Page childPage;

@inject
@Via("resource")
private String linkLabel;

@inject
@Via("resource")
private String linkPath;


@SlingObject
ResourceResolver resourceResolver;

@Deleted Account
SlingHttpServletRequest request;

@Override
public String getLinkLabel() {
return linkLabel;
}
@Override
public String getLinkPath() {


//childTitle=linkPath;
return linkPath;

}

@PostConstruct
protected void init() {

logger.info("linkLabel ====== " + linkLabel);
logger.info("linkPath ============ " + linkPath);
//String children = linkPath;
ResourceResolver resourceResolver = request.getResourceResolver();
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
//ArrayList pageList = new ArrayList();
Page rootPage = pageManager.getPage(linkPath);
logger.info("rootPage === ++++++++++++ " + rootPage);
//Page rootPage = linkPath;
Iterator<Page> rootPageIterator = rootPage.listChildren(new PageFilter(false, false), false);
while(rootPageIterator.hasNext())
{

childPage = rootPageIterator.next();
String childTitle = childPage.getTitle();
logger.info("childTitle === "+childTitle);

}

}


}