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.
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
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ụ:
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
- Sling không tìm file
.htmltrê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ự:
- Property
sling:resourceTypetrên resource. - Nếu không có →
jcr:primaryTypecủa node (ví dụcq:Page→ resource typecq/Page).
Với AEM page, property nằm trên jcr:content, không nằm trên node cq:Page:
Cơ chế thực tế khi request /content/corporate/jobs/developer.html:
- Resource là node
cq:Page, không cósling:resourceType→ fallback resource typecq/Page. - AEM có script
/libs/cq/Page/Page.jspcho resource type này. - Script đó include resource
jcr:content; từ đây Sling resolve tiếp theosling:resourceTypecủ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ự:
- Candidate khớp nhiều selector hơn; nếu bằng số selector thì candidate khớp extension thắng.
- Candidate có resource type gần nhất với resource type được request (đi lên theo chuỗi super type).
- 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):
- 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:
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:
Thứ tự chọn:
Điểm đáng chú ý:
print/a4.jsp(5) thắngprint.html.jsp(6): khớp nhiều selector hơn quan trọng hơn khớp extension.a4.html.jspkhông phải candidate vì selector đầu tiên không phảiprint.a4/print.html.jspkhô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 extensionhtml).
4. sling:resourceSuperType
resourceSuperType tạo chuỗi kế thừa resource type, không phải kế thừa Java class.
Chuỗi resolve:
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 địnhsling/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
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
pathtương đối được resolve từ resource hiện tại; absolute path bắt đầu bằng/.<sling:include>tương đươngdata-sly-resource, không phảidata-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:
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
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:
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:
6.4 Ví dụ
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ớ
Nguồn
- Sling URL decomposition: https://sling.apache.org/documentation/the-sling-engine/url-decomposition.html
- Sling servlets và script resolution: https://sling.apache.org/documentation/the-sling-engine/servlets.html
- Sling scripting và script naming: https://sling.apache.org/documentation/bundles/scripting.html
- SlingPostServlet: https://sling.apache.org/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html
- Cách AEM page được resolve: https://cqdump.joerghoh.de/2019/01/07/how-does-sling-resolve-an-aem-page-to-the-correct-resource-type/