For AI agents: the complete documentation index is available at https://nhanbe.id.vn/llms.txt, the full documentation bundle is available at https://nhanbe.id.vn/llms-full.txt, and this page is available as Markdown at https://nhanbe.id.vn/aem/articles/sling/sling-framework.md.

Sling Framework - Basic

Nền tảng Sling trong AEM: URL decomposition, resource resolution, script resolution, resource super type, include và SlingPostServlet.

Sling Framework

1. Sling là gì?

Sling là framework web resource-centric: URL được map tới một resource, sau đó Sling chọn script/servlet để render resource đó. Trong AEM, resource thường là một node JCR.

Cách tiếp cận truyền thốngSling
URL → file script (PHP, JSP)URL → resource (node JCR)
Script tự lấy dữ liệu từ DBResource đã mang sẵn property và child node
Script quyết định hiển thị gìsling:resourceType quyết định script
GET /content/corporate/jobs/developer.html
→ resolve resource /content/corporate/jobs/developer
→ đọc resource type (với page AEM: sling:resourceType trên jcr:content)
→ tìm script/servlet khớp resource type + selectors + extension + method
→ script nhận resource hiện tại để render

Không có "controller" trung tâm. Resource type, selectors, extension và HTTP method quyết định code nào chạy.


2. URL decomposition

Ví dụ: GET /content/corporate/jobs/developer.print.a4.html/a/b?x=12

Thành phầnGiá trịLấy trong codeÝ nghĩa
Resource path/content/corporate/jobs/developerresource.getPath()Resource cần render
Selectorsprint, a4requestPathInfo.getSelectors()Biến thể render, có thứ tự
ExtensionhtmlrequestPathInfo.getExtension()Định dạng output / script engine
Suffix/a/brequestPathInfo.getSuffix()Dữ liệu bổ sung, không tham gia resolve resource
Queryx=12request.getParameter("x")Tham số request

Quy tắc cần nhớ:

  • Resource path là prefix dài nhất khớp với một resource đang tồn tại (sau khi áp mapping), và ký tự tiếp theo phải là dấu chấm hoặc hết URL. Không thể suy ra ranh giới này chỉ bằng cách nhìn URL, vì tên node có thể chứa dấu chấm.
  • Selectors nằm giữa dấu chấm đầu tiên sau resource path và dấu chấm cuối cùng trước / hoặc cuối URL.
  • Extension nằm sau dấu chấm cuối cùng đó.
  • Suffix chỉ được nhận khi sau resource path có ít nhất một dấu chấm.

Ví dụ:

URIResource pathSelectorsExtensionSuffix
/a/b.html/a/b–html–
/a/b.s1.s2.html/a/bs1.s2html–
/a/b.s1.html/c/d/a/bs1html/c/d
/a/b./c/d/a/b––/c/d

print.a4 khác a4.print: thứ tự selector có ý nghĩa trong cả script naming lẫn servlet registration.


3. Request → Resource → Script

3.1 Resource resolution

Tìm thấyKhông thấyHTTP requestDecompose URL: resourcepath, selectors, extension,suffixResourceResolver.resolve(path)ResourceNonExistingResourceDefault servlet → 404
  • Sling không tìm file .html trên disk. Nó resolve path trong repository (hoặc qua resource provider khác).
  • Với AEM, path thường là node JCR; resource có thể là cq:Page, cq:PageContent, nt:unstructured, dam:Asset...
  • Không tìm thấy resource → NonExistingResource; nếu không có servlet/filter xử lý riêng, default servlet trả 404.

3.2 Resource type: sling:resourceType và fallback

Resource type là thứ Sling dùng để tìm code render. Thứ tự:

  1. Property sling:resourceType trên resource.
  2. Nếu không có → jcr:primaryType của node (ví dụ cq:Page → resource type cq/Page).

Với AEM page, property nằm trên jcr:content, không nằm trên node cq:Page:

<!-- /content/corporate/jobs/developer/.content.xml -->
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0"
          xmlns:cq="http://www.day.com/jcr/cq/1.0"
          xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
          jcr:primaryType="cq:Page">
    <jcr:content
        jcr:primaryType="cq:PageContent"
        sling:resourceType="hr/jobs"
        jcr:title="Developer Position"/>
</jcr:root>

Cơ chế thực tế khi request /content/corporate/jobs/developer.html:

  1. Resource là node cq:Page, không có sling:resourceType → fallback resource type cq/Page.
  2. AEM có script /libs/cq/Page/Page.jsp cho resource type này.
  3. Script đó include resource jcr:content; từ đây Sling resolve tiếp theo sling:resourceType của page component (hr/jobs).

Đây là lý do đọc sling:resourceType trên node page sẽ không thấy, và cũng là lý do các selector/extension đặc biệt của page có thể cần overlay trong /libs/cq/Page.

resourceType thường là relative path (hr/jobs), được resolve theo search path (/apps/hr/jobs, /libs/hr/jobs); cũng có thể là absolute path.

3.3 Script/servlet resolution

Scripts và servlets được xử lý giống nhau. Khi có nhiều candidate, Sling chọn theo thứ tự:

  1. Candidate khớp nhiều selector hơn; nếu bằng số selector thì candidate khớp extension thắng.
  2. Candidate có resource type gần nhất với resource type được request (đi lên theo chuỗi super type).
  3. Service ranking (với servlet đăng ký qua OSGi).

Với mỗi resource type trong chuỗi, Sling tìm trong từng search path (/apps trước, rồi /libs):

type = resource.getResourceType()
while (type != null) {
    for (root : searchPaths) findScripts(root + type)

    if (type == "sling/servlet/default") {
        type = null
    } else {
        type = getResourceSuperType(type) ?: "sling/servlet/default"
    }
}
  • Resource type không khai báo sling:resourceSuperType → mặc định là sling/servlet/default (tương tự java.lang.Object).
  • Không tìm được script/servlet nào, kể cả default → 404 (nếu resource không tồn tại) hoặc 500.

3.4 Script naming

Tên script có hai dạng:

{resourceTypeLabel}.{requestExtension}.{requestMethod}.{scriptExtension}
{selectorPath}.{requestExtension}.{requestMethod}.{scriptExtension}
Thành phầnQuy tắc
resourceTypeLabelSegment cuối của resource type (ví dụ hr/jobs → jobs). Có thể bỏ nếu có requestExtension.
requestExtensionExtension của request. Có thể bỏ nếu là html.
requestMethodGET/HEAD có thể bỏ; method khác bắt buộc.
scriptExtensionBắt buộc: html cho HTL, jsp cho JSP.
selectorPathSelectors nối bằng /. Nếu script khai báo ít selector hơn request, các selector đó phải là selector đầu tiên.

Lưu ý: .esp là engine JavaScript (Rhino) cũ, không dùng trong AEM. Dùng .html (HTL) hoặc .jsp.

3.5 Ví dụ thứ tự ưu tiên

Resource type sling/sample, request GET ...print.a4.html:

1. GET.jsp
2. sample.jsp
3. html.jsp
4. print.jsp
5. print/a4.jsp
6. print.html.jsp
7. print/a4.html.jsp

Thứ tự chọn:

7 > 5 > 6 > 4 > 3 > 2 > 1

Điểm đáng chú ý:

  • print/a4.jsp (5) thắng print.html.jsp (6): khớp nhiều selector hơn quan trọng hơn khớp extension.
  • a4.html.jsp không phải candidate vì selector đầu tiên không phải print.
  • a4/print.html.jsp không phải candidate vì sai thứ tự selector.
  • Với HTL, tên tương ứng thường là print/a4.html, print.html, sample.html (bỏ request extension html).

4. sling:resourceSuperType

resourceSuperType tạo chuỗi kế thừa resource type, không phải kế thừa Java class.

<!-- /apps/mysite/components/custom-page/.content.xml -->
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0"
          xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
          jcr:primaryType="cq:Component"
          sling:resourceSuperType="mysite/components/base-page"/>

Chuỗi resolve:

resource type = mysite/components/custom-page
  /apps/mysite/components/custom-page/...
  /libs/mysite/components/custom-page/...
resource type = mysite/components/base-page        (super type)
  /apps/mysite/components/base-page/...
  /libs/mysite/components/base-page/...
resource type = sling/servlet/default              (mặc định)
  /apps/sling/servlet/default/...
  /libs/sling/servlet/default/...

Thứ tự là type trước, search path sau: toàn bộ candidate của component con được xét trước khi đi lên super type.

  • Script có ở component con → dùng script con.
  • Script không có ở con → rơi xuống super type.
  • Mỗi resource type trong chuỗi đều được tìm trên tất cả search path (/apps, /libs).
  • Không có sling:resourceSuperType → mặc định sling/servlet/default, không phải "không có gì".

Dùng để tách component base dùng chung và component con chỉ override phần cần thiết.


5. Ghép resource: data-sly-resource và sling:include

5.1 HTL

<!-- Include resource khác, dùng resource type của nó -->
<div data-sly-resource="${'header'}"></div>

<!-- Include resource khác nhưng ép resource type -->
<div data-sly-resource="${'header' @ resourceType='mysite/components/header'}"></div>

<!-- Include một script (không resolve resource) -->
<div data-sly-include="'footer.html'"></div>
  • data-sly-resource → include resource, chạy đầy đủ script resolution (selectors, extension, resource type).
  • data-sly-include → include script, không đi qua resource resolution.

5.2 JSP

<%@ taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling" %>

<sling:include path="header"/>
<sling:include path="/content/shared/footer"/>
  • path tương đối được resolve từ resource hiện tại; absolute path bắt đầu bằng /.
  • <sling:include> tương đương data-sly-resource, không phải data-sly-include.
  • Không có tag <sling:include> trong HTL.

6. SlingPostServlet

POST tới một resource được xử lý mặc định bởi SlingPostServlet. Đây là cách tạo/sửa/xoá node mà không cần viết servlet riêng.

6.1 Operation

Điều khiển bằng :operation:

:operationÝ nghĩa
(không set hoặc rỗng)Tạo mới hoặc modify resource tại URL
deleteXoá resource
moveMove sang :dest
copyCopy sang :dest
importImport JSON/XML/ZIP
nopKhông làm gì, chỉ trả status
checkin / checkoutVersioning

Không có :operation=create hay :operation=modify. Việc tạo/sửa là hành vi mặc định khi không set operation.

6.2 Parameter điều khiển

ParameterÝ nghĩa
:destĐích cho move/copy
:applyToÁp dụng cho nhiều resource (delete/copy/move), hỗ trợ * ở segment cuối
:replaceCho phép ghi đè đích của copy/move/import
:orderĐổi thứ tự child node: first, last, before x, after x, hoặc số thứ tự
:redirectRedirect 302 sau khi thành công
:status=browserTrả 200 thay vì status thật (dùng cho form trên browser)
:name, :nameHintTên node khi URL kết thúc bằng / hoặc /*

Riêng :contentType chỉ dùng cho :operation=import (json, xml, zip...), không phải content type của response.

6.3 Property và hậu tố @

Tên parameter là relative path của property:

TênTác dụng
title hoặc ./titleSet property trên resource hiện tại
../titleSet property trên node cha
/content/x/titleSet property theo absolute path

Nếu request có parameter bắt đầu bằng ./, SlingPostServlet chỉ xử lý các parameter có prefix ./, ../ hoặc /; các parameter không prefix bị bỏ qua.

Hậu tố điều khiển:

Hậu tốTác dụng
@TypeHintÉp kiểu JCR (Long, Boolean, Date, String[]...)
@DefaultValueGiá trị mặc định nếu không có value
@UseDefaultWhenMissingDùng default kể cả khi parameter không được submit (checkbox)
@IgnoreBlanksBỏ qua value rỗng trong multi-value
@ValueFromLấy value từ parameter khác
@DeleteXoá property/node
@MoveFrom / @CopyFromMove/copy item từ path khác
@PatchThêm (+value) / bớt (-value) phần tử của multi-value property

6.4 Ví dụ

<!-- Sửa property trên jcr:content -->
<form method="POST" action="/content/mysite/en/page/jcr:content">
    <input name="./jcr:title" value="Trang mới">
    <input name="./hideInNav" value="true">
    <input name="./hideInNav@TypeHint" value="Boolean">
    <input name=":redirect" value="/content/mysite/en/page.html">
</form>

<!-- Xoá property -->
<form method="POST" action="/content/mysite/en/page/jcr:content">
    <input name="./pageTitle@Delete" value="true">
</form>

<!-- Xoá node -->
<form method="POST" action="/content/mysite/en/old-page">
    <input name=":operation" value="delete">
</form>

Với page (cq:Page), tạo bằng SlingPostServlet cần set jcr:primaryType, jcr:content với cq:PageContent và cq:template. Thực tế nên dùng Page Manager API hoặc công cụ của AEM thay vì tự ghép form.


7. Điểm cần nhớ

ĐiểmNội dung
ResourceAbstraction của Sling; trong AEM thường là node JCR
Resource pathPrefix dài nhất khớp resource tồn tại, không phải "cắt tại dấu chấm đầu tiên"
Resource typesling:resourceType, fallback jcr:primaryType
Pagesling:resourceType nằm trên jcr:content, không nằm trên cq:Page
Script engineHTL .html, JSP .jsp; .esp là engine cũ, không dùng
Ưu tiênNhiều selector hơn thắng; bằng nhau thì xét extension; sau đó resource type gần nhất, rồi service ranking
Super typeKế thừa resource type; mặc định sling/servlet/default
Includedata-sly-resource (resource) khác data-sly-include (script)
POSTSlingPostServlet; : điều khiển operation, @ điều khiển property
Search path/apps trước /libs

Nguồn