The AEM – Sling connection simplified

Apache Sling is a web framework based on the JCR API. We have seen that the content in AEM is saved as nodes and properties in Oak – a reference implementation of JCR. In most simple term, Sling, in AEM’s context, helps you to issue http requests and render content from Oak repository appropriately.

Use Template Sling from my GitHub account.

Sling uses scripts or servlets to render content. Sling uses the node’s sling:resourceType property to determine the script that renders the content. Let us see what happens when you access a url.

Assume that you access the following url: http://localhost:4502/content/aem-company.html. First the request goes to /content/aem-company. The node has a sling:resourceType property that points to aem-company/components/homepage. Here Sling gets the location of the script or servlet that needs to be used to render the content. It checks for the location in the apps folder and locate the node. And, then render the content based on the default script, body.html. I have explained how body.html becomes the default script in one of the previous posts. Have a look at that.

  1. Now create another script in aem-company/components/homepage. (test.html)
  2. Add a test content: html test page
  3. Let us access the page again with a different url: http://localhost:4502/content/aem-company.test.html

We changed the url by adding test.html which specifies the default script that needs to be used to render the content. (It’s called a selector.) In this way, you can manipulate the url and provide content accordingly. For example, using the same url, but different selector, like  product.html and product.details.html you can provide different and meaningful content.

  1. Create another script at the same location. (details.jsp)
  2. Add a test content: jsp test page
  3. Access the same using the following url: http://localhost:4504/content/aem-company.details.html

See that the page is rendered. In the same location I mentioned above (/apps/aem-company/components/homepage), Sling searches for details.html, obviously couldn’t find it. It then used details.jsp to render the content.

This kind of script resolution mechanism Sling uses is important for you to know. See a cheat sheet Adobe prepared:
Take a look at this page – there are plenty of information. I hope you got a general idea about Sling now. Consider it as a starting point & happy learning.

See you soon.

Comments are closed.