Create your first sling model in AEM
I'd be glad to explain Sling models in AEM, their structure, and how to create your first one:
Sling Models in AEM
Sling models are a powerful concept in AEM development that simplifies data access and manipulation for your components. They are essentially Plain Old Java Objects (POJOs) annotated with specific annotations to interact with Sling objects (resources, request objects, etc.) and OSGi services. Sling models offer several benefits:
- Improved Code Readability: Separating data access logic from component logic leads to cleaner and easier-to-understand code.
- Reduced Boilerplate Code: Annotations eliminate the need for manual resource and property access, streamlining development.
- Increased Reusability: Sling models can be reused across components, promoting consistency and reducing redundancy.
- Simplified Data Access: Models provide convenient access to JCR properties, request attributes, and OSGi services through annotations.
Structure of a Sling Model:
A Sling model is a Java class with annotations that define its behavior. Here's a breakdown of the key elements:
@Model
annotation: This annotation marks the class as a Sling model. It can contain properties like:adaptables
: Specifies the Sling objects the model can adapt to (e.g.,Resource
,SlingHttpServletRequest
).resourceType
: Optionally associates the model with a specific JCR resource type.
- Annotation-based Injectors: You use annotations to inject data into the model:
@ValueMapValue
: Injects a specific JCR property value.@OptionalValueMapValue
: Similar to@ValueMapValue
, but allows the property to be absent.@RequestAttribute
: Injects a request attribute value.@OSGiService
: Injects an OSGi service dependency.
- Class Members: Define variables to hold the injected data.
Creating Your First Sling Model in AEM
Develop the Model Class:
- Create a new Java class in your project.
- Annotate the class with
@Model
. - Specify the
adaptables
property to indicate what Sling objects it can adapt to (typicallyResource
orSlingHttpServletRequest
). - Define variables with annotations like
@ValueMapValue
to inject specific JCR properties.
(Optional) Associate with Resource Type:
- If your model is specific to a certain resource type, set the
resourceType
property in the@Model
annotation to the corresponding JCR path.
- If your model is specific to a certain resource type, set the
(Optional) Inject Request Attributes or OSGi Services:
- If your model requires information from request attributes or OSGi services, use
@RequestAttribute
or@OSGiService
annotations respectively.
- If your model requires information from request attributes or OSGi services, use
Package the Class:
- Include the model class in your AEM project's Java package structure.
Deploy (if developing locally):
- Deploy your changed code to the AEM author instance using your preferred method (e.g., package manager, Git integration).
Example:
@Model(adaptables = Resource.class)
public class MySlingModel {
@ValueMapValue("jcr:title")
private String title;
@ValueMapValue("myProperty")
@OptionalValueMapValue
private String myProperty;
public String getTitle() {
return title;
}
public String getMyProperty() {
return myProperty;
}
}
Using the Sling Model in a Component (Sightly Example):
<h1 data-sly-text="model.title"></h1>
<p data-sly-text="model.myProperty ? model.myProperty : 'Property not found'">...</p>
This code retrieves the title
and myProperty
from the Sling model and displays them in the component.
By following these steps and understanding the structure of Sling models, you can effectively leverage them to streamline your AEM development.
Comments
Post a Comment