I worte java code to display list of child pages using sling models . It's not working getting error. Please help me on it | Community
Skip to main content
May 5, 2024
Solved

I worte java code to display list of child pages using sling models . It's not working getting error. Please help me on it

  • May 5, 2024
  • 2 replies
  • 963 views

package com.company.digital.core.models;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Named;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.RequestAttribute;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;


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

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

@Model(adaptables = Resource.class, defaultInjectionStrategy=DefaultInjectionStrategy.OPTIONAL)

public class ChildPagesModel {

@ValueMapValue
private String parentPath;

@SlingObject
private SlingHttpServletRequest request;

@SlingObject
private ResourceResolver resolver;

@SlingObject
private Resource resource;

@SlingObject
private Page page;

@PostConstruct
protected void init() {
resolver = request.getResourceResolver();
resource = resolver.getResource(parentPath);
page = resource.adaptTo(Page.class);
}

public List getChildPages() {
List<String> list = new ArrayList<String>();
Iterator<Page> itr = page.listChildren();

while (itr.hasNext()) {
list.add(itr.next().getPath());
}
return list;
}
}

 

HTL code:

-----------

Child Pages <br>
<sly data-sly-use.data="com.company.digital.core.models.ChildPagesModel"/>
<sly data-sly-list="${data.getChildPages}">
${item}
</sly>

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by EstebanBustamante

@ramyamo1 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community

2 replies

sravs
Community Advisor
Community Advisor
May 6, 2024

@ramyamo1 ,

 

Whenever you are calling any field value(s) using getter method in sigtly you need to specify the variable name like,

 

<sly data-sly-list="${data.childPages}">

 

methods invocations can use shortened method syntax for example: ${model.getTitle()} can be shorted to ${model.title}.

 

Please refer sling model examples https://medium.com/@toimrank/aem-sling-models-ea0fb2606625

https://www.aemcq5tutorials.com/tutorials/sling-model-sightly-aem/

https://experienceleague.adobe.com/en/docs/experience-manager-learn/getting-started-wknd-tutorial-develop/project-archetype/component-basics#sling-models

 

Hope this helps!!!

ramyamo1Author
May 7, 2024

Hi @sravs ,

Thanks for responding!!

I tried your suggestion, got same error.

Attaching the code & error message below. Did I miss anything? Please advise.

 

Thanks!!


Code: -

 

package com.company.digital.core.models; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.annotation.PostConstruct; import javax.inject.Named; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.RequestAttribute; import org.apache.sling.models.annotations.injectorspecific.ScriptVariable; import org.apache.sling.models.annotations.injectorspecific.SlingObject; import org.apache.sling.models.annotations.injectorspecific.ValueMapValue; import com.day.cq.wcm.api.Page; import org.apache.sling.models.annotations.DefaultInjectionStrategy; @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class ChildPagesModel { @ValueMapValue private String parentPath; @SlingObject private SlingHttpServletRequest request; @SlingObject private ResourceResolver resolver; @SlingObject private Resource resource; @SlingObject private Page page; @PostConstruct protected void init() { resolver = request.getResourceResolver(); resource = resolver.getResource(parentPath); page = resource.adaptTo(Page.class); } public List getChildPages() { List<String> list = new ArrayList<String>(); Iterator<Page> itr = page.listChildren(); while (itr.hasNext()) { list.add(itr.next().getPath()); } return list; } }

Sightly code: -

 

Child Pages <br> <sly data-sly-use.data="com.company.digital.core.models.ChildPagesModel"/> <sly data-sly-list="${data.childPages}"> ${item} </sly>

Error message:-

 

sravs
Community Advisor
Community Advisor
May 7, 2024

Hi @ramyamo1, I can see you are using improper injector specific annotation for the objects. Please refer https://sourcedcode.com/blog/aem/aem-sling-model-injectors-annotations-cheat-sheet-reference-guide#SlingObject

 

and please try below snippet for your use case.

package com.company.digital.core.models; import com.day.cq.wcm.api.PageManager; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.ScriptVariable; import org.apache.sling.models.annotations.injectorspecific.ValueMapValue; import com.day.cq.wcm.api.Page; import org.apache.sling.models.annotations.DefaultInjectionStrategy; @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class AnchorModel { @ValueMapValue private String parentPath; @ScriptVariable private PageManager pageManager; public List<String> getChildPages() { List<String> list = new ArrayList<>(); Page page = pageManager.getPage(parentPath); Iterator<Page> itr = page.listChildren(); while (itr.hasNext()) { list.add(itr.next().getPath()); } return list; } }

 Hope this helps!!!

EstebanBustamante
Community Advisor and Adobe Champion
EstebanBustamanteCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
May 8, 2024

@ramyamo1 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community

Esteban Bustamante