Sling Framework - Basic
1. Sling là gì?
Sling là một framework RESTful được xây dựng trên JCR Repository (nơi lưu trữ nội dung).
🎯 Điểm khác biệt quan trọng nhất:
Ví dụ cụ thể:
# Framework thông thường (ví dụ: PHP)
https://myapp.com/product.php?id=123
→ Gọi trực tiếp file product.php
→ Trong file PHP: SELECT * FROM products WHERE id = 123
# Sling
https://mysite.com/content/products/laptop.html
→ Tìm node /content/products/laptop trong JCR
→ Đọc property sling:resourceType = "myapp/components/product"
→ Tìm script tại /apps/myapp/components/product/product.esp
→ Script nhận sẵn resource (node laptop) để render
Lợi ích:
- Tách biệt nội dung và presentation
- Mỗi resource có thể có nhiều cách render (selectors, extension)
- Dễ dàng customize từng page mà không cần thay đổi code
2. URL Decomposition (Phân tích URL)
Đây là phần quan trọng nhất để hiểu Sling hoạt động.
Ví dụ URL:
https://myhost/tools/spy.printable.a4.html/a/b?x=12
Phân tích từng phần:
3. Quy trình xử lý request (Từ URL → Content → Script)
Bước 1: Tìm Resource (Content Node)
Ví dụ:
Request: /content/corporate/jobs/developer.html
1. Tìm node tại /content/corporate/jobs/developer.html → Không có
2. Tìm node tại /content/corporate/jobs/developer → Có!
→ Resource là node /content/corporate/jobs/developer
Bước 2: Lấy sling:resourceType
Từ node tìm được, đọc property sling:resourceType.
Ví dụ:
<!-- Node: /content/corporate/jobs/developer -->
<jcr:root>
<jcr:primaryType>cq:Page</jcr:primaryType>
<sling:resourceType>hr/jobs</sling:resourceType>
<jcr:title>Developer Position</jcr:title>
<!-- ... các properties khác ... -->
</jcr:root>
Bước 3: Tìm Script phù hợp nhất
Sling sẽ tìm script dựa trên:
- Resource Type:
hr/jobs
- HTTP Method: GET, POST, PUT, DELETE
- Extension: html, pdf, json, v.v.
- Selectors: print, a4, mobile, v.v.
Nơi tìm script:
/apps/hr/jobs/ (ưu tiên cao nhất)
/libs/hr/jobs/ (fallback)
4. Script Resolution (Cách Sling chọn script)
Quy tắc ưu tiên:
Sling chọn script cụ thể nhất (nhiều selector match nhất).
Ví dụ chi tiết:
Request:
/content/corporate/jobs/developer.print.a4.html
với sling:resourceType = "hr/jobs"
Các script có thể có tại /apps/hr/jobs/:
1. GET.esp
2. jobs.esp
3. html.esp
4. print.esp
5. print.html.esp
6. print/a4.esp
7. print/a4/html.esp
8. print/a4.html.esp
Thứ tự ưu tiên (cao → thấp):
(8) print/a4.html.esp ← ✅ CHỌN (match chính xác nhất)
(7) print/a4/html.esp
(6) print/a4.esp
(5) print.html.esp
(4) print.esp
(3) html.esp
(2) jobs.esp
(1) GET.esp
Cách đặt tên script:
5. Resource Super Type (Kế thừa script)
Đây là tính năng rất mạnh mẽ, cho phép tái sử dụng script.
Cách hoạt động:
<!-- Node: /content/my-page -->
<jcr:root>
<sling:resourceType>myapp/components/custom-page</sling:resourceType>
</jcr:root>
<!-- Node: /apps/myapp/components/custom-page -->
<jcr:root>
<sling:resourceSuperType>myapp/components/base-page</sling:resourceSuperType>
</jcr:root>
Hierarchy:
/my-page
→ resourceType = myapp/components/custom-page
→ resourceSuperType = myapp/components/base-page
→ resourceSuperType = sling/servlet/default (mặc định)
Kết quả:
Khi Sling tìm script cho /my-page, nó sẽ tìm theo thứ tự:
/apps/myapp/components/custom-page/ (của resource type)
/apps/myapp/components/base-page/ (của super type)
- Mặc định của Sling
Lợi ích:
- Tạo component cha (base) với logic chung
- Component con override chỉ những phần cần thay đổi
- Dễ dàng maintain và mở rộng
6. sling:include (Ghép nhiều component)
Khi bạn muốn ghép nhiều component vào một page, dùng sling:include.
Cú pháp:
<!-- JSP -->
<sling:include resourceType="geometrixx/components/image/img" />
<!-- HTL -->
<sling:include resourceType="geometrixx/components/image/img" />
<!-- Với path cụ thể -->
<sling:include path="/content/shared/footer" />
Cách hoạt động:
Ví dụ thực tế:
Page structure:
/content/my-page
/jcr:content
/header (sling:resourceType = myapp/components/header)
/main (sling:resourceType = myapp/components/main-content)
/sidebar (sling:resourceType = myapp/components/sidebar)
/footer (sling:resourceType = myapp/components/footer)
Script của page:
<!-- /apps/myapp/components/page/page.jsp -->
<html>
<body>
<!-- Include header -->
<sling:include path="header" />
<!-- Include main content -->
<sling:include path="main" />
<!-- Include sidebar -->
<sling:include path="sidebar" />
<!-- Include footer -->
<sling:include path="footer" />
</body>
</html>
7. SlingPostServlet (Xử lý POST requests)
Sling cung cấp SlingPostServlet để xử lý các request POST với nhiều tham số ẩn mạnh mẽ.
Các tham số đặc biệt:
Ví dụ:
<!-- Form tạo node mới -->
<form method="POST">
<input name=":operation" value="create" />
<input name="./title" value="My New Page" />
<input name="./sling:resourceType" value="myapp/components/page" />
<input name=":redirect" value="/content/my-page.html" />
</form>
<!-- Form xóa node -->
<form method="POST">
<input name=":operation" value="delete" />
</form>
8. Tóm tắt nhanh
9. Lưu ý quan trọng
-
KHÔNG gọi script trực tiếp: Sling không cho phép truy cập trực tiếp vào script (ví dụ: /apps/myapp/component/page.jsp). Luôn phải thông qua resource.
-
Ưu tiên /apps hơn /libs: Khi tìm script, Sling luôn tìm trong /apps trước, sau đó mới đến /libs.
-
HTL được khuyến nghị: Adobe khuyến nghị sử dụng HTL (HTML Template Language) thay vì JSP cho component.
-
Tính RESTful: Mọi thứ đều là resource, việc tương tác với resource (GET, POST, PUT, DELETE) được xử lý tự động.