Skip to main content

Day 10: Getting to know Sling Models


AEM Developer Series

Wouldn't it be great if you have a direct way to map your JCR node properties to your model class? I guess it would be. In AEM, we can achieve this via Sling Models.
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. 

Feel free to befriend me on Facebook, Twitter or Linked In or say Hi by email.

Happy Coding 😊

Comments

Post a Comment

Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.
Adobe is way ahead in this war and has gained a lot of market capture. They are leaders in the Digital Marketing platforms since 2014-15. One of the flagship product in Adobe's Digital Marketing suite is Adobe Experience Manager (AEM).
Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.
When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the res…

Day 01: Introduction to AEM

Day 04: Developing first OSGi bundle