UnderstandingAEM mapping request to resource

Locate content resource

Resource path is used to locate content resource

  1. Sling checks weather a node exists at the location specified in the request(e.g. /content/articles/category-name/article-name.print.a4.html)
  2. If no node is found ,the the extension is dropped and search repeated (e.g. /content/articles/category-name/article-name.print.a4)
  3. If no node is found then return 404 http code

Locate scripts once content resource is located

  • The sling:resourceType is used to locate script to be used for rendering the content.
  • Scripts are located in /apps or /libs.
  • If certain rest method(GET, POST) is required, it should be specified in upercase within script name(e,g. a4.POST.jsp).
  • If multiple scripts apply for a given request

The script with the best match is selected. The more specific a match is ,the better it is; in other word, the more selector matched the better, regardless of any request extension or method name match.

Rules:

  1. Folder(e.g. nodes of tyope nt:folder) takes precedence over jsp file names when resolving using selectors, at least the first selector.
  2. Only one selector in a file name has effect, any file name contains two selectors don’t ever get selected, but name of folder can be used to match the selector in the request.
  3. Scripts with HTTP method names(e.g. GET.jsp) is selected as a last resort even after the default script(examples.jsp)
  4. The precedence of same name files are html > jsp > esp. E.g. if examples.html and examples .jsp both exist,then the examples.html will be shown.

For example: http://www.aemtreasury.com/content/resolution.print.a4.html/a/b?name=Dale of type sling:resourceType=”dale/example”

Assuming scripts structure as diagram below:

jcr_resolution

The order of preference should be as shown:

  1. /apps/dale/example/print/a4.html.jsp
  2. /apps/dale/example/print/a4/html.jsp
  3. /apps/dale/example/print/a4.jsp
  4. /apps/dale/example/print.html.jsp
  5. /apps/dale/example/print.jsp
  6. /apps/dale/example/html.jsp
  7. /apps/dale/example/example.jsp
  8. /apps/dale/example/GET.jsp

I prefer to make the script name is the same with fold name which is easy to remember, in this case is /dale/example/example.jsp

Note

How to create without template

If you want to create a page without template you can do that but it’s not possible using siteadmin console as it’s content page component uses templates to create pages.

  1. Create a node of type cq:Page and save,
  2. Add cq:PageContent node(named jcr:content) under page node
  3. Add/Copy required properties manually.

Understand AEM URL decomposition

Rules of URL decomposition are list below

1. Resource Path

  • The longest substring of the request URL such that the resource path is either the complete request URL
  • Or the next character in the request URL after the resource path is a dot (.), which means the part between host and the first dot in the whole request

2. Selectors

  • If the first character in the request URL after the resource path is a dot,
    the string after the dot up to but not including the last dot before the next slash character or the end of the request URL.
  • If the resource path spans the complete request URL no seletors exist.
  • If only one dot follows the resource path before the end of the request URL or the next slash, also no selectors exist.

3. Extension

  • The string after the last dot after the resource path in the request URL but before the end of the request URL
  • Or the next slash after the resource path in the request URL.

4. Suffix Path

  • If the request URL contains a slash character after the resource path and optional selectors and extension, the path starting with the slash up to the end of the request URL is the suffix path.
  • Otherwise, the suffix path is empty. Note, that after the resource path at least a dot must be in the URL to let Sling detect the resource path.

5. Params

  • The key-value after queston mark(?).

For example, with the URL: http://www.aemtreasury.com/articles/category-name/article-name.print.a4.html/a/b?name=Dale

the URL can be broken down into composite parts:

protocol host path selector extension suffix params
https http://www.aemtreasury.com articles/category-name/article-name print.a4 html / a/b ? name=Dale

References:

  1. “URL Decompositon” section in https://docs.adobe.com/docs/en/aem/6-0/develop/the-basics.html
  2. https://sling.apache.org/documentation/the-sling-engine/url-decomposition.html