Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

SlingModel @ChildResource is not working

Avatar

Avatar
Validate 1
Level 2
vijays80591732
Level 2

Likes

6 likes

Total Posts

56 posts

Correct Reply

4 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 3
View profile

Avatar
Validate 1
Level 2
vijays80591732
Level 2

Likes

6 likes

Total Posts

56 posts

Correct Reply

4 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 3
View profile
vijays80591732
Level 2

27-07-2020

Hi All,

 

I have a node called "entries" under the page. The "entries" node having some child nodes like entry1, entry2, entry3. The "entries" and child nodes "entry1" have been creating via code and I don't have any component for this. Now, I'm trying to access the "entries" node in a sling model but it's not working. Below is the screenshot of the node structure,

 

Screenshot 2020-07-28 at 12.08.19 PM.png

 

Below is the code,

 

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

private static final Logger LOGGER = LoggerFactory.getLogger(HistoryModel.class);

@Self
private Resource currentResource;

/*@ChildResource(name = "entries")
private List<HistoryBean> entries;
*/
@ChildResource(name = "entries")
private Resource entries;

@ChildResource(name="entries",injectionStrategy= InjectionStrategy.OPTIONAL,via = "resource")
Resource child;

public Resource getEntries() {
return entries;
}

public Resource getChild() {
return child;
}

@PostConstruct
protected void postConstruct() {
//Nothing inside
LOGGER.debug("HistoryModel post construct Method().. calling..");
LOGGER.debug("HistoryModel entries.. calling..{}", entries);
LOGGER.debug("HistoryModel child.. calling..{}", child);

}

Tried with @childResource. In the log, I'm getting null. anything wrong in the code? Can anyone help?

 

@arunpatidar26 @smacdonald2008 @arunpatidar26 @Ratna_Kumar @kautuk_sahni @wimsymons @cqsapientu69896 @vanegi @Veena_Vikram @varuns7990 @Theo_Pendle

 

Thanks,

Vijay

AEM 6.4 Sling Model

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
ChitraMadan
MVP

Likes

227 likes

Total Posts

124 posts

Correct Reply

50 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back
View profile

Avatar
Establish
MVP
ChitraMadan
MVP

Likes

227 likes

Total Posts

124 posts

Correct Reply

50 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back
View profile
ChitraMadan
MVP

28-07-2020

Hi @vijays80591732 ,

 

If this is not a component and you want to access it in Sling Model, then you can make use of a java class to read its child values and return in Sling Model in this way:

 

@Getter
@Setter
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class HistoryModel {

@Inject
private List<WorkflowItem> workflowItems;

@Self
private transient Resource resource;

@PostConstruct
private void init() {
workflowItems = SomeUtilClass.getWorkflowItems(resource, "path to entries node");
}
}
 
And your Util Class method can have this code:
List<WorkflowItem>workflowItemsList = Collections.emptyList();
Resource workflowResource = resource.getResourceResolver().getResource("path to entries");
if (workflowResource != null && workflowResource.hasChildren()) {
workflowItemsList =
Lists.newArrayList(workflowResource.getChildren()).stream()
.map(child -> child.adaptTo(WorkflowItem.class))
.collect(Collectors.toList());
return workflowItemsList;
}
 
WorkflowItem class can be another Model class with Description and Message and other attributes as per your need.

Answers (1)

Answers (1)

Avatar

Avatar
Springboard
MVP
Suraj_Kamdi
MVP

Likes

142 likes

Total Posts

156 posts

Correct Reply

41 solutions
Top badges earned
Springboard
Validate 1
Establish
Contributor
Ignite 3
View profile

Avatar
Springboard
MVP
Suraj_Kamdi
MVP

Likes

142 likes

Total Posts

156 posts

Correct Reply

41 solutions
Top badges earned
Springboard
Validate 1
Establish
Contributor
Ignite 3
View profile
Suraj_Kamdi
MVP

28-07-2020

@vijays80591732 

try with 

@ChildResource(injectionStrategy = InjectionStrategy.OPTIONAL)

Collection<Resource> entries

 

Hope this will help