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 development of content-oriented applications.

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

URL Decomposition


Consider below URL’s :

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

Lets break it down the url into its composite parts:

protocolhostcontent pathselector(s)extension suffix param(s)
http://www.mywebsite.comproducts/product1.printable.a4html/a/b?x=12
http://localhost:4502content/aemTutorials/sightlyPage.testhtml?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.

Sling resource resolution in aem

Step-1: Double click on a page in siteadmin , to open respective page in 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 content folder in crx de.
  • /aemTutorials refers to the package under contents folder.
  • /sightlyPage refers to our 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 above figure it is at training/components/myComponent , 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 render it else it drops the extension and search 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 above example, lets add few more  files to myComponent component under apps.

sling-resource_resolution_preference

  • When we create a component, then aem creates a default rendering script by .jsp extension i.e myComponent.jsp
  • Lets create one more file with name myComponent.html.

Note:- Sling decides rendering of script on the basis of selector + extension . First priority goes to selector, if no selector is available then priority goes to 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 selector.
  • Lets 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

Lets 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 parent component and again run above 5 rules for script resolution.

Hope You got a clear idea of How Sling Resolution is done in AEM. If you have any further doubt, Please leave a comment.

 

 

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *