March 15, 2020
Estimated Post Reading Time ~

How Sling Resource Resolution is done in AEM

The focus of this tutorial is to learn how Apache Sling Resource Resolution is done in AEM. AEM uses Sling to develop web applications over Java content repository. Apache Sling is a Web framework for the development of content-oriented applications.

In this tutorial, I have tried to provide very easy, detailed and step by step explanation of how to sling resource resolution is done in AEM. After completing this tutorial you will have a clear understanding of:


URL Decomposition

Consider below URL:
Dummy URL– http://www.mywebsite.com/products/product1.printable.a4.html/a/b?x=12
Actual URL – http://localhost:4502/cf#/content/aemTutorials/sightlyPage.test.html?x=12
Let's break it down the url into its composite parts:
protocol
host
content path
selector(s)
extension

suffix

param(s)
http://
www.mywebsite.com
products/product1
.printable.a4
html
/
a/b
?
x=12
http://
localhost:4502
content/aemTutorials/sightlyPage
.test
html
?
x=12

Sling Resource Resolution – Mapping URL to Respective JCR Node :


How a url is mapped to corresponding jcr node in aem. How Sling resource resolution is done. The complete Sling Resource Resolution is shown in below diagram.

Step-1: Double click on a page in siteadmin, to open the respective page in a browser.
Step-2-3-4-5:  Analyse the URL /contents/aemTutorials/sightlyPage it is known as content path.
·       The /contents path refers to the path of the content folder in crx de.
·       /aemTutorials refer to the package under the contents folder.
·       /sightlyPage refers to the page that we have created under siteadmin.
·       jcr:content of sightlyPage contains a property sling:ResourceType. Which tells sling where our component is located. As shown in the above figure it is a training/components/myComponent, which means sling needs to check for myComponent under /apps folder.
Step-6: Analyse selector and extension test.html
·   Sling appends the selector and extension after the component name i.e myComponent.test.html. If it found this component then it renders it else it drops the extension and searches again.

Sling Resource Resolution Preference

I have recently received a query that:
If the component contains .html and .jsp files, then the first preference goes to html or JSP?
Further enhancing the above example, let's add a few more files to myComponent component under apps.
·       When we create a component, then aem creates a default rendering script by .jsp extension i.e myComponent.jsp
·   create one more file with the name myComponent.html.
Note:- Sling decides rendering of script on the basis of selector + extension. The first priority goes to the selector if no selector is available then priority goes to the extension.
Now if we double click our page http://localhost:4502/cf#/content/sample/testing.html then by default myComponent.jsp script will be called because here .html is an extension not selector. Means while resolving the script we see for extension in URL(HTML), in crxde we compare it with node name(myComponent) We can ignore the last part .jsp from node name. Its only relevance is .jsp gets priority over .html(sightly) file.
Between jsp and html, first priority goes to jsp as by default a component is linked to jsp at the time of creation.
·   Now we add one more file html.jsp, now if we double click the page then html.jsp will be rendered as here it receives html as a selector.
·   Let's add one more file by name myComponent.html.jsp, now if we double click the page then myComponent.html.jsp will be rendered because here it receives myComponent.html as the selector
So the final priority order will be myComponent.html.jsp–>html.jsp–>myComponent.jsp –>myComponent.html

Let's decompose above analysis in simple point, Below is the priority of resolution in Descending order(Highest to lowest):-

1.    Selector +Extension .jsp –> test.myComponent.html.jsp
2.    Selector.jsp –> test.jsp
3.    Extension.jsp –>html.jsp
4.    Node Name.jsp –> myComponent.jsp
5.    method name.jsp(Based on type of request we make Either GET or POST) –> GET.jsp or POST.jsp
Note:-If no match is found then it will look for sling:resourceSuperType if available on the component, then it goes to the parent component and again runs above 5 rules for script resolution.



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.