Howdy fellow developers 👋! In the last post, we learned basics of HTL and clientlibs. Both HTL and clientlibs are responsible for rendering an AEM component on the frontend. There are times where we need to perform complex business logic on the backend and AEM is no exception.
AEM uses the OSGi framework for its backend activities and as we know from part 2 of this developer series, we write our Java code in OSGi bundle and deploy it to AEM.
In this post, we will create a simple component and perform some business logic on it using Java and based on that we will render the output on the page. The Java code will reside in a special model class known as Sling Model.
Wouldn’t it be great if you have a direct way to map your JCR node properties to your model class? Say what? Yeah, of course, it would be 😎. Sling Models lets us do this in AEM.
Sling Models are “pure” POJOs which map Sling objects (resources, request objects etc.).
Since Sling Models are annotation-driven Plain Old Java Objects (POJOs), annotations are used a lot. They allow us to map resource properties, assign default values, inject OSGi services and much more.
The most simplest form of using Sling Models is in which the class is annotated with @Model, and with one adaptable. The fields which need to be injected are annotated with @Inject.