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
  • 6889 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

Thanks @himanshu_singhal, as you may have noticed, i haven't closed the resource resolver but wish there was doc on it for sling models specifically where we obtain resource resolver  from resource.


Apart from using below.. i wish there was a direct  way via annotations to get the properties, do you know?..   Page currentPage = pageManager.getContainingPage(resource);

if slinghttprequest is used, have you tried to obtain properties via annotations?

 

 


  • There's no need to close resolver. Only service created resource-resolver shall be closed and that too if used outside the context of "try-with-resource".
  • To get the properties - if you're using Resource adaptable then you can use @586265 to get the property value. If you're working with Request adaptable then you can use @ValueMapValue to get the property value inside sling model. However, keep in mind that you directly get the property associated with resource via which sling model is invoked. Technically, we call sling model w.r.t. resource for which operation has to be performed. If you need to get any property that is different from resource from which sling model is called, then we usually try to get the resource first and then get the property out of it. 


    I hope it helps!