--**Flow Summary (HTL):*1. `data-sly-use` points to a class annotated with `@Model`. 2. Sling creates an instance and injects fields. 3. HTL use
Sling Model
Table of Contents
1. Role
Flow Summary (HTL):
data-sly-usepoints to a class annotated with@Model.- Sling creates an instance and injects fields.
- HTL uses
${model.property}.
2. Minimal Model
Java (core module, example com.mysite.core.models.TitleModel):
HTL:
3. adaptables: Resource vs SlingHttpServletRequest
4. Injection Annotations Review
4.1. @ValueMapValue — Reading Properties
4.2. @ChildResource — Child Nodes / Multifields
Multifield with Nested Model:
4.3. @Self — The Adaptable Instance
Request-based: @Self SlingHttpServletRequest request — used for query parameters, etc.
Security: Request parameters are untrusted inputs; sanitize before using in queries, redirects, or authorizations.
4.4. @OSGiService
Requires an active OSGi service; injected interface must match registered type.
4.5. @ScriptVariable (Request Adaptable Only)
4.6. @RequestAttribute — Parameters from HTL
4.7. @ResourcePath
4.8. @Via — Injecting from Resource when Adaptable is Request
5. @Default — Choosing the Right Attribute by Type
6. @PostConstruct — Post-Injection Initialization
AEM 6.5: uses javax.annotation.PostConstruct (Java 8 and 11 on AEM 6.5 stack).
7. Optional Injection
8. Interfaces + adapters (Clean API for HTL & Testing)
HTL using interface:
9. JSON Export (Headless / SPA) — .model.json
Requirements Overview:
Skeleton Example:
10. Unit Testing (wcm.io Aem Mocks, JUnit 5)
11. Quick Troubleshooting
12. Non-Request Contexts: Service Users + Repoinit (On-Prem 6.5)
Repoinit Example Configuration:
ui.config/.../org.apache.sling.jcr.repoinit.RepositoryInitializer~mysite.cfg.json
Service User Mapping:
org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended~mysite.cfg.json
Code:
13. Security Summary
14. Summary
- Sling Model = Java class annotated with
@Modelfor Sling to inject values from JCR/request/OSGi, invoked in HTL viadata-sly-use. - Prefer
Resource.classwhen sufficient; useSlingHttpServletRequestwhen global HTL objects or request data are needed. - Use
DefaultInjectionStrategy.OPTIONAL+@Defaultfor stable authoring environments. - Use Interfaces +
adaptersto separate API definitions and simplify testing. .model.json: RequiresComponentExporter+@Exporter+resourceType.- Outside HTTP requests: Use service user + repoinit + mapping; avoid admin sessions.