I'm trying to fetch the child pages when I enter the path in the link. But it's not working
Below is the java code:
@Model(adaptables = Resource.class,defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Test {
@ValueMapValue
private String filePath;
@OSGiService
private ResourceResolver resourceResolver;
@Inject
Resource items;
public Resource getItems() {
Resource resource=resourceResolver.getResource("/content/second/us/en/jcr:content/root/container/container/testone");
filePath=resource.getValueMap().get("filePath",String.class);
items= resourceResolver.getResource(filePath);
return items;
}
}
Below is the html code:
<sly data-sly-use.model="com.secondsite.core.models.Test"></sly>
<pre data-sly-test="${properties.linkdetails==dynamic}">
<ul data-sly-list.item="${model.items.listChildren}">
<li>${item.title}</li>
</ul>
</pre>
<pre data-sly-test="${properties.linkdetails==manual}">
<a href="${properties.pagelink @ extension='html'}">${properties.pagetitle}</a>
</pre>
Please help
Solved! Go to Solution.
Views
Replies
Total Likes
Please check the path field(dialog name) given in the component dialog and change the code accordingly.
Hi,
There are so many problems with above code. I would recommend you to go through with Sling Model and HTL documentation to understand sling model injection and htl comparison.
https://sling.apache.org/documentation/bundles/models.html
https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md
Hi @Sanjana12 ,
You have to improve your code quality with the help of the document provided by @arunpatidar .
For your use case, Please refer to the below sample code.
Test Class:
import com.adobe.learning.core.bean.TestBean;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageFilter;
import com.day.cq.wcm.api.PageManager;
import org.apache.sling.api.resource.Resource;
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.injectorspecific.SlingObject;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Test {
//Injecting the filePath authored in component dialog
@Inject
private String filePath;
@SlingObject
private ResourceResolver resourceResolver;
List<TestBean> pageList;
@PostConstruct
protected void init() {
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
assert pageManager != null;
Page rootPage = pageManager.getPage(filePath);
//Fetching all child page
Iterator<Page> rootPageIterator = rootPage.listChildren(new PageFilter(false, false), true);
pageList = new ArrayList<>();
//Getting path of all child pages
while (rootPageIterator.hasNext()) {
TestBean testBean = new TestBean();
testBean.setTitle(rootPageIterator.next().getTitle());
testBean.setPagePath(rootPageIterator.next().getPath());
pageList.add(testBean);
}
}
//Getting the list of pages
public List<TestBean> getPageList() {
return pageList;
}
}
TestBean Class :
public class TestBean {
private String title;
private String pagePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPagePath() {
return pagePath;
}
public void setPagePath(String pagePath) {
this.pagePath = pagePath;
}
}
Sightly Code :
<sly data-sly-use.model="com.secondsite.core.models.Test"></sly>
<div data-sly-list.pageDetails="${model.pageList}">
<a href="${pageDetails.pagePath @ extension='html'}">${pageDetails.title}</a>
</div>
You can also check out this article for the list of child pages - https://aeminmyway.blogspot.com/p/how-to-display-list-of-children-under.html
Hope this could help you !!!
Thanks
Shiv
the above java code is throwing error. I need to fetch all the child pages on entering the link in the pagefield. That's my requirement.
Please check the path field(dialog name) given in the component dialog and change the code accordingly.
The above code is working fine. Thanks
Hi @Sanjana12 ,
The one @Shiv_Prakash_Patel gave would absolutely work.
I find that you are new to AEM development and I would like to have you the following code snippets respectively.
Dialog:
<filePath jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/pathbrowser" fieldLabel="Parent Page" name="./filePath"/>
Component Html:
<sly data-sly-use.model="com.secondsite.core.models.Test"/> <ul data-sly-list.xyz="${model.childrenWithRootPage}"> <li> ${xyz.getTitle} </li> </ul>
Sling model:
import com.day.cq.wcm.api.Page; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.SlingObject; 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 = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class Test { @Inject @Default(values = "/content/second/us/en") private String filePath; @SlingObject ResourceResolver resourceResolver; private static final Logger logger = LoggerFactory.getLogger(Test.class); public List<Page> getChildrenWithRootPage() { Page parentPage = resourceResolver.getResource(filePath).adaptTo(Page.class); logger.info("Parent page is ::" + parentPage.getPath()); List<Page> children = new ArrayList<>(); Iterator<Page> childPages = parentPage.listChildren(); while (childPages.hasNext()){ Page childPage = childPages.next(); children.add(childPage); logger.info("Child page is added to list::" + childPage.getPath()); } return children; } }
I made this as simple as possible, so that you understand without any extra java knowledge.
Please note that, this wouldn't be a production level code, the key here is to make you understand the flow.
In order to see what is happening in the back end as well, I strongly recommend you to run your instance on debug mode.
command to start in debug:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar <your-jar-name>.jar
Views
Likes
Replies
Views
Likes
Replies