Sling Models with Sightly Part – V (Key Annotations – III)

In this post, I will answer some of the queries asked by my blog readers. This blog will also help you in getting knowledge of How you access cq:defined Object in Sling Model classes?
Before answering these question, I want to make sure that you guys are using latest Sling Model dependency and AEM version 6.x. So let’s start-

Q1. How will I get properties object in Sling Model Class?
I think there is no need to get this properties object as you can directly inject the resource properties ( already described in my previous blog) but if you still want to do that then, you have to make two changes.

First use adaptable as SlingHttpServletRequest.class as shown below-

Then you can directly inject properties as shown below-

Q2. During the build time, I am getting a Maven build error as shown below-

How to resolve this issue?
For resolving this issue,  you need to add one more maven dependency into your project the dependency name is
cqcommens and it’s Maven dependency is-

Q3. How will I get inherited page properties object in Sling Model class?
For getting this object in your Sling Model class, you need to inject a field as shown below-

With SlingHttpServletRequest.class as an adaptable value.

Q4. Can I use Resouce.class as well as slingHttpServletRequest.class as adaptable in the same class?
Yes, you can do that. Here is the syntax-

Q5. How will I get page object in Sling Model class?
For getting this object in your Sling Model class, you need to inject a field as shown below-

With SlingHttpServletRequest.class as an adaptable value.

Q6. Do you have any working Sling model class example that implements all these properties?
Yes, here it is-

Q7. How am I testing these annotations in Sling Model class?
I have created a dummy component and that component calls these Sling Model classes. Sightly code snippet is-

Happy Coding..!!

Ankur Chauhan
Tech Lead