AEM Developer Series
- Day 00: AEM Developer Series
- Day 01: Introduction to AEM
- Day 02: AEM Architecture
- Day 03: Setting up AEM Development Environment
- Day 04: Developing First OSGi Bundle
- Day 05: Working with Sling Servlets in AEM
- Day 06: Playing with Sling Post Servlet
- Day 07: Creating your first component in AEM
- Day 08: Dueling with JavaScript Use API
- Day 09: Dueling with Java User API
- Day 10: Getting to know Sling Models
- Day 11: Client Libraries in Action
- Day 12: Creating your custom OSGi Configuration
- Day 13: Schedulers in AEM
- Day 14: Eventing in AEM
- Day 15: Custom Workflows in AEM
- Day 16: Creating JMX Beans in AEM
- Day 17: Working with QueryBuilder API
- Day 18: Working with Granite Datasources in AEM
- Day 19: Replication API in Action
- Day 20: Working with Users and Groups in AEM
Sling Models are "pure" POJOs which maps Sling objects (resources, request objects etc.).
Since Sling Models are annotation-driven Plain Old Java Objects (POJOs), annotations are used a lot. They allow you to map resource properties, assign default values, inject OSGi services and much more.
Without making further delay, let's see the concepts in action.
Working with Sling Models
For AEM 6.2
- Download the latest Sling Models API and Implementation bundles from here and upload them to ./system/console/bundles
- In the project’s pom files, make sure the version numbers for Sling Models API and Implementation are updated based on the ones installed in your AEM server.
- In your core module pom.xml file, check for maven-bundle-plugin, and make sure you have all packages that contain the model classes or interfaces in header Sling-Model-Packages, so that your models can be picked up.
For AEM 6.3+
- Since AEM 6.3 is built on Sling 1.3, we don't have to install any bundles in our AEM server.
- But we need to make sure that the Sling Models API version in AEM matches with the one in our project's pom files.
Example
Now let us look at an example for sling models 😮
- Create a new AEM project using AEM Multimodule Project (how? see here) and deploy in your AEM instance.
- Go to CRXDE and under your project in /apps create a new component with the following configuration
![]() |
Create a Component |
- Now create the cq:dialog node under the component node with the following configuration.
- Create a new class UserModel.java in your project and paste the following code into it
- Let us understand the code of the class. We are using @Model annotation to specify that this model class is a Sling Model. Each data member is annotated with @Inject. This class will be mapped to a resource in JCR.
- Drag and drop the component on the page and configure it
![]() |
Configure the component |
- After saving the component, the data will save under the page node as shown in the figure
![]() |
Data stored in JCR |
- To access this resource in the JCR, let us create a Sling Servlet (what is it? see here) and paste the following code in it.
- In line #54-55, we are getting the resource and in line #60, we are adapting the resource to our UserModel class
- Note that we are not setting any data in the model object, we are just calling the getters to access the data stored in the JCR.
- Now, deploy the code in your AEM server (how? see here) and hit the request - http://<host>:<port>/bin/slingmodel/user you will see the following result.
Data stored in the resource is:
First Name: Anirudh Last Name: Sharma Gender: Male Country: India
- As you see, we can directly access a Sling Resource using Sling Model classes. This saves us a lot of boilerplate code and lets us do separation of concerns.
Conclusion
Congratulations!! 🙋 you have duelled with Sling Model classes. I hope you enjoyed this post.
You can find the complete code of this project on my GitHub. Feel free to fork or open issues, if any.
I would love to hear your thoughts on this and would like to have suggestions from you to make it better.
Happy Coding 😊
cq:dialog code is missing
ReplyDeleteThanks for pointing that out. Fixed it.
DeleteIn component don't we need to "data-sly-use" the model class, Without that how it will bind the dialog and the model?
ReplyDeleteIf we want to display data in component html page then we have to use sightly annotation ie. as mentioned "data-sly-use.test="your model class"" but here as mentioned by Anirudh we are accessing the data via servlet call , so its not required . It depends upon scenario when to use what. :)
DeleteLooks like replica of https://helpx.adobe.com/experience-manager/using/sling_models.html
ReplyDelete