Day 4 of 40
Apache Sling
Resource resolution, URL decomposition, selectors and suffixes
By the end of today you should be able to
- Explain how Sling resolves a URL to a resource and then to a rendering script
- Decompose a Sling URL into path, selectors, extension and suffix
- Describe the role of sling:resourceType and sling:resourceSuperType
- Predict which script renders a given request
Sling turns content into a request handler
Apache Sling is the layer between the repository and the HTTP request. Its founding idea is inverted from most web frameworks: instead of a route table that maps URLs to controllers, Sling maps the URL to a resource in the repository, then asks the resource what should render it. Content is king, and the content decides its own renderer.
Request URL → resolve to a resource → read its sling:resourceType → find a script or servlet registered for that type → render.
URL decomposition
A Sling URL carries more structure than it looks. Take:
/content/wknd/us/en/home.print.a4.html/archive/2024?debug=true
\__________________________/ \_______/ \__/ \____________/ \________/
resource path selectors ext suffix query
- Resource path — the node Sling resolves to:
/content/wknd/us/en/home. - Selectors — dot-separated tokens (
print,a4) that let one resource render several ways. Cacheable, unlike query parameters. - Extension —
html,json,csv. Chooses the output format. - Suffix — everything after the extension, passed to your code as extra path information. Also cacheable.
- Query string — the only part the Dispatcher will usually refuse to cache.
Need a JSON variant of a page for a widget? Use a selector — page.feed.json — not page.html?format=json. Selectors are part of the path, so the Dispatcher can cache each variant as a separate file. Query strings usually blow straight through the cache.
Resource types: content pointing at code
Every renderable node carries a sling:resourceType property, whose value is a path
(usually relative) to a component: wknd/components/title. Sling looks for that under
/apps first, then /libs. That search order is exactly what makes the
overlay mechanism work — your /apps copy wins over Adobe's /libs original.
sling:resourceSuperType declares inheritance. A component that sets
sling:resourceSuperType = "core/wcm/components/title/v3/title" inherits that component's
scripts and behaviour, and can override just the pieces it wants. This is the mechanism behind the
Core Components proxy pattern you will meet on day 13.
Script resolution
Given resource type wknd/components/title and a request for .html, Sling looks for a script in /apps/wknd/components/title/ in roughly this order of specificity:
title.html— matches the component name (the usual case)html.html— matches the extension- With selectors, more specific names win: a request for
.print.htmlprefersprint.htmlovertitle.html - HTTP method matters too:
POST.htmlhandles POSTs - If nothing matches, Sling walks up the
sling:resourceSuperTypechain
Servlets can also register for a resource type in Java, via
@SlingServletResourceTypes. Registering a servlet by resource type is strongly
preferred over registering by path, because path-bound servlets bypass the repository's access
control — a real security consideration you will revisit on day 32.
Resource vs Node
Sling wraps JCR nodes in a Resource abstraction. You mostly work with
Resource, ResourceResolver and ValueMap rather than raw JCR
APIs — they are simpler and not every resource is backed by a node. resource.getValueMap()
gives you typed access to properties with defaults, which is what Sling Models use under the hood.
Sling is content-driven routing. The URL finds a node; the node's sling:resourceType finds the code; selectors and suffix let one node render many ways while staying cacheable.
Watch
Adobe's own videos for this topic. They load only when you press play.
Read on Experience League
The primary sources these notes are drawn from.
Your notes
Saved automatically to this browser.
Check yourself
11 questions on today's material. 80% to pass.