Skip to main content

Sling Resource Resolution: How Requests Map to Content

Updated 3 min readMarkdown
Part of seriesApache Sling Series

Apache Sling is at the heart of AEM’s web framework. One of its most powerful features is resource resolution, the process that maps HTTP requests to content in the repository and then to the scripts or servlets that generate the response.

Understanding Sling resource resolution is key to writing maintainable and scalable AEM applications. It explains why your URLs work the way they do, how scripts are selected, and how the framework dynamically adapts to changes in content.


1. What Is Resource Resolution?

Resource resolution is the process Sling uses to determine:

  1. Which resource (JCR node) corresponds to an HTTP request

  2. Which script or servlet will render or process that resource

Every URL in AEM eventually maps to a resource in the JCR, and understanding this mapping is critical for custom routing, servlets, and templates.


2. How Sling Resolves a Request

Given a request URL:

http://localhost:4502/content/mysite/en/home.html

Sling resolves it step by step:

  1. Resource Path

    • /content/mysite/en/home is mapped to a JCR node (cq:Page + jcr:content)
  2. Selectors and Extension

    • .html → extension

    • .print.html → selectors (optional)

  3. Resource Type

    • Each resource has a sling:resourceType property

    • Maps to a script or servlet to render the resource

  4. Script Resolution

    • Sling searches /apps → /libs for scripts matching:
      /<resourceType>.<selector>.<extension> or /<resourceType>.<extension>

3. URL Anatomy in Sling

/content/mysite/en/home.print.a4.html
|--------|--------|-------|----|
 Path    Selector  Suffix  Extension
  • Path → Maps to the JCR node

  • Selector → Optional instructions for the script (e.g., print)

  • Suffix → Extra info (ignored by Sling for resolution)

  • Extension → Determines the script type (html, json, xml)

Example: home.print.a4.html could render a print-ready version of a page using a specific script.


4. Resource Mapping Rules

  1. Primary resource: The JCR node at the requested path

  2. Fallback: Sling will traverse parent nodes if resource is not found

  3. Aliases: Nodes can define sling:alias to map multiple URLs to a resource

  4. Vanity URLs: /home.html → /content/mysite/en/home via sling:vanityPath


5. Script Resolution Order

When Sling looks for a script to render a resource:

  1. Check resourceType of node (sling:resourceType)

  2. Look in /apps/<resourceType> first

  3. If not found, fall back to /libs/<resourceType>

  4. Match selectors and extension

  5. If multiple scripts match, Sling uses the most specific

This order allows project-level overrides in /apps without modifying /libs (best practice in AEM projects).


6. Servlets and Resource Resolution

  • Sling maps not just scripts, but also servlets to resources

  • Servlets can be registered using:

    • sling.servlet.resourceTypes → Bind to a resource type

    • sling.servlet.selectors → Optional selectors

    • sling.servlet.extensions → Optional extensions

Example: A servlet registered for sling:resourceType = mysite/components/page and selector = print will handle /content/mysite/en/home.print.html.



7. Key Takeaways

  • Every HTTP request in Sling maps to a resource in the repository

  • Resource resolution considers path, selectors, suffix, extension, resource type, scripts, and servlets

  • /apps overrides /libs for customization

  • Understanding resource resolution is crucial before building servlets, Sling Models, or REST endpoints

  • Vanity URLs and aliases make the site flexible and SEO-friendly


What’s Next?

In the following post, we’ll dive into Sling Servlets, explaining how servlets are registered, how selectors and extensions influence routing.

No comments yet. Be the first to comment.