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 @Via but in vain
Is it wise to use both adaptables?
Thanks in advance
Solved! Go to Solution.
Views
Replies
Total Likes
@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-eac...
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-r...
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.
Views
Replies
Total Likes
@NitroHazeDev Yes, it's absolutely fine to use both adaptables.
Views
Replies
Total Likes
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;
Views
Replies
Total Likes
@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-eac...
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-r...
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.
Views
Replies
Total Likes
@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;
}
}
Views
Replies
Total Likes
@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.
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?
Views
Replies
Total Likes
Views
Replies
Total Likes
Views
Likes
Replies