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>
Solved! Go to Solution.
Views
Replies
Total Likes
@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
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/
Hope this helps!!!
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:-
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#S...
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!!!
Hi @sravs ,
Thanks for your replay.
I tried above code , got same error.
I forgot to mention "SlingHttpServletRequest.class" in @model adaptables. Now it's working.
package com.company.digital.core.models;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
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.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, SlingHttpServletRequest.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<String> getChildPages() {
List<String> list = new ArrayList<String>();
Iterator<Page> itr = page.listChildren();
while (itr.hasNext()) {
list.add(itr.next().getPath());
}
return list;
}
}
Thanks!!
@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