Getting error while accessing current resource | Community
Skip to main content
Level 3
March 24, 2024
Solved

Getting error while accessing current resource

  • March 24, 2024
  • 4 replies
  • 1402 views

Hi Everyone,

 

I am trying to create a multifield dialog using sling model but while accessing the values, my code is failing to get current resource. My code is failing inside try block , i am getting null value of current resource. Please help me to understand and resolve this issue.

 

package com.site.myproj.core.models.impl;

import com.site.myproj.core.models.MultifieldAccordionFAQModel;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
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.ValueMapValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Model(
adaptables = SlingHttpServletRequest.class,
adapters = MultifieldAccordionFAQModel.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)

public class MultifieldAccordionFAQModelImpl implements MultifieldAccordionFAQModel {

private static final Logger LOG = LoggerFactory.getLogger(MultifieldAccordionFAQModelImpl.class);

@Inject
Resource componentResource;

@ValueMapValue
@Default( values = "FAQ's Default")
private String accordionHeading;

@Override
public String getAccordionHeading() {
return accordionHeading;
}


@Override
public List<Map<String, String>> getAccordionDetails() {

List<Map<String,String>> accordionDetailsMap = new ArrayList<>();
try{
LOG.debug("Component Resource>> {}", componentResource);
Resource accordionParentNode = componentResource.getChild("accordionDetails");
LOG.debug("accordionParentNode >> {}", accordionParentNode);
if(accordionParentNode != null){
for( Resource accordion : accordionParentNode.getChildren()){

Map<String, String> accordionsMap = new HashMap<>();

accordionsMap.put("accordionTitle",accordion.getValueMap().get("accordionTitle", String.class));
accordionsMap.put("accordionDescription",accordion.getValueMap().get("accordionDescription", String.class));

accordionDetailsMap.add(accordionsMap);
}
}
}catch ( Exception e){
LOG.debug("<< Error occured during MultifieldAccordionModelImpl Implementation >> {}", e.getMessage());
}

return accordionDetailsMap;
}
}

 

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 anupampat

Hi @monish_gavali ,

You can get the resource like below in a sling model,

@SlingObject private Resource resource;

Hope it helps. 

4 replies

DPrakashRaj
Community Advisor
Community Advisor
March 24, 2024
anupampat
Community Advisor
anupampatCommunity AdvisorAccepted solution
Community Advisor
March 25, 2024

Hi @monish_gavali ,

You can get the resource like below in a sling model,

@SlingObject private Resource resource;

Hope it helps. 

Level 3
March 25, 2024

Thanks @anupampat 

abhishekanand_
Community Advisor
Community Advisor
March 25, 2024

 

@586265 private Resource resource; public List<Map<String, String>> getAccordionDetails() { List<Map<String,String>> accordionDetailsMap = new ArrayList<>(); try { Resource accordionParentNode = resource.getChild("accordionDetails"); LOG.debug("Accordion Parent Node: {}", accordionParentNode); if (accordionParentNode != null) { for (Resource accordion : accordionParentNode.getChildren()) { Map<String, String> accordionsMap = new HashMap<>(); accordionsMap.put("accordionTitle", accordion.getValueMap().get("accordionTitle", String.class)); accordionsMap.put("accordionDescription", accordion.getValueMap().get("accordionDescription", String.class)); accordionDetailsMap.add(accordionsMap); } } } catch (Exception e) { LOG.error("Error occurred while fetching accordion details: {}", e.getMessage()); } return accordionDetailsMap; }

i have injected the resource using the @586265 annotation.
The resource field now holds the reference to the injected resource.
The method attempts to retrieve the child resource named “accordionDetails” from the injected resource.
If the “accordionDetails” resource exists, it proceeds to iterate over its children (individual accordion items).

Hope this helps!

 

Abhishek Anand
Level 3
March 25, 2024

Thank you so much @arunpatidar for the solution, I am able to do this using @Self annontation. Before that i tried with 

@SlingObject

 so it is also working.