Code sample to get the list of nested child pages at a given path in json format using componentexporter
Solved! Go to Solution.
Views
Replies
Total Likes
Check the below code snippet, hope this is what you are expecting.
@SlingObject
ResourceResolver resourceResolver;
@Self
SlingHttpServletRequest request;
private List<String> children = new ArrayList<String>();
private String root = "/content/test";
@PostConstruct
protected void initialize() {
Page rootPage = resourceResolver.getResource(root).adaptTo(Page.class);
Iterator<Page> listChildren = rootPage.listChildren(null, true);
while(listChildren.hasNext()) {
children.add(listChildren.next().getPath());
}
}
public List<String> getChildren() {
return children;
}
public String getRoot() {
return root;
}
Can you please elaborate on your question? It is not making sense...
Views
Replies
Total Likes
The component exporter's purpose is to output JSON from your sling model's properties via Apache Sling provides a Jackson JSON exporter. What you can do is write a recursive method that will traverse through each and every node, from the root node, and save each parent within a List<Page> object. Once your List<Page> is generated, you can simply create a getter for List<Page>, and calling model.json, you will get a JSON representation of all the child pages, recursively.
Take a look at ACS Common's sitemap generator, as they recusively traverse through every node to construct the sitemap.xml, https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/ad...
Views
Replies
Total Likes
Please use Page APi in your model to get all the page and create json based on results
Check the below code snippet, hope this is what you are expecting.
@SlingObject
ResourceResolver resourceResolver;
@Self
SlingHttpServletRequest request;
private List<String> children = new ArrayList<String>();
private String root = "/content/test";
@PostConstruct
protected void initialize() {
Page rootPage = resourceResolver.getResource(root).adaptTo(Page.class);
Iterator<Page> listChildren = rootPage.listChildren(null, true);
while(listChildren.hasNext()) {
children.add(listChildren.next().getPath());
}
}
public List<String> getChildren() {
return children;
}
public String getRoot() {
return root;
}