Sling Models and adaptables- SlingHttpServletRequest and Resource | Community
Skip to main content
Level 9
December 27, 2022
Solved

Sling Models and adaptables- SlingHttpServletRequest and Resource

  • December 27, 2022
  • 1 reply
  • 6884 views

Hi,

 

I had couple questions, i wanted to get the current page path as well as the properties under page/jcr:content/image/fileReference.

Option1- If i use a resource as adaptable, i can get file reference but unable to get current page path, the resource.getPath points to the jcr:content of the page.

If i use the resource resolver from the resource marked with @self, do i have to close it? Is it wise?

PageManager pageManager= resource.getResourceResolver().adaptTo(PageManager.class);
Page currentPage = pageManager.getContainingPage(resource);

 

2. If i use SlingHttpServletRequest and Resource, i am not able to get the properties under page/jcr:content/image/fileReference while i could get the currentpage path.
Tried @2434638 but in vain

 

 

Is it wise to use both adaptables?

 

Thanks in advance

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 Himanshu_Singhal

@nitrohazedev 
You're right about the part of not using both adaptables however in most of the use cases it doesn't even cause any issue. Explanation is given here:
https://stackoverflow.com/questions/68838818/aem-slingmodels-why-do-we-need-unused-adaptables-in-each-model-why-need-both

Option 2 - Above link should provide you explanation. 

Option 1 - The approach is okay. There's no need at all to close the resolver. Only service resolver should be closed and that should if not "try-with-resource"
https://cqdump.joerghoh.de/2018/11/14/try-with-resource-or-i-will-never-forget-to-close-a-resource-resolver/

Since your sling model is invoked at page level and injected resource points to sling resource type which is linked to jcr:content node of page and that's the reason you get the following when you try to get resource.getPath()

So, as you're already shared piece of code to get the page out of it - it's correct. 

1 reply

Himanshu_Singhal
Community Advisor
Community Advisor
December 27, 2022

@nitrohazedev Yes, it's absolutely fine to use both adaptables. 

Level 9
December 27, 2022

@himanshu_singhal 

Thank you for your response, i felt the same. The reason for the ask is to get to the best practices cause i happened to see a blog mentioning it is not per the best practice

 

Option2-Is there a way to get the values with both adaptables? I am not able to get values with Slinghttpservlet request and resource adaptables for the properties under jcr:content/image/@fileReference


With option1 is the approach ok to get the page path and do i have to close the resource resolver explicitly? and is it the right way to get to the resource resolver? i know of another via annotations
@SlingObject
private ResourceResolver resolver;

 

Himanshu_Singhal
Community Advisor
Community Advisor
December 28, 2022

@himanshu_singhal  So the jist would be to not use both. I figured it was a mess while playing around a while back after i responded

Also, how do i get the properties via annotations for properties under jcr:content/image say?

 

 just to summarize, below is ok please let me know? with no closing resource resolver? @himanshu_singhal 

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

@Self
private Resource resource;

@SlingObject
private ResourceResolver resolver;

 

@Inject
@Optional
@Named("image/fileReference")
private String thumbUrl;

 

private String extPageUrl;

 

public String getExtPageUrl() {
PageManager pageManager= resolver.adaptTo(PageManager.class);
Page currentPage = pageManager.getContainingPage(resource);
logger.info("Page model  {}",currentPage.getPath());

//do some processing
return this.extPageUrl;
}

}


@nitrohazedev Yes, your code seems alright. 
Also, instead of individual prop of child resource, if you wish to obtain entire resource that also you can do using annotation 

@ChildResource
private Resource image;

That will give you control over entire resource and whatever properties you wish to fetch. However, if there's only 1 prop value needed in that case, injecting is another alternative you've chosen.