Sling Models and adaptables- SlingHttpServletRequest and Resource | Adobe Higher Education
Skip to main content
Level 9
December 27, 2022
Beantwoord

Sling Models and adaptables- SlingHttpServletRequest and Resource

  • December 27, 2022
  • 1 reactie
  • 6884 Bekeken

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

Er kunnen geen reacties meer worden geplaatst op dit onderwerp.
Beste antwoord door 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 reactie

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 27, 2022

@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.