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-

@Model(adaptables = {SlingHttpServletRequest.class})

Then you can directly inject properties as shown below-

private ValueMap properties;
Q2. During the build time, I am getting a Maven build error as shown below-
 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project blog-bundle: Compilation failure: Compilation failure:
[ERROR] /D:/accunity/blog/bundle/src/main/java/sling/models/[3,34] package does not exist
[ERROR] /D:/accunity/blog/bundle/src/main/java/sling/models/[31,13] cannot find symbol
[ERROR] symbol:   class InheritanceValueMap
[ERROR] location: class sling.models.CQDefinedObjectImplModel
[ERROR] /D:/accunity/blog/bundle/src/main/java/sling/models/[39,33] cannot access
[ERROR] class file for not found
[ERROR] -> [Help 1]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :blog-bundle
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-

private InheritanceValueMap pageProperties;

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-

@Model(adaptables = {SlingHttpServletRequest.class,Resource.class})
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-

private Page resourcePage;

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-

@Model(adaptables = {SlingHttpServletRequest.class,Resource.class})
public class CQDefinedObjectImplModel {

    private Logger logger = LoggerFactory.getLogger(CQDefinedObjectImplModel.class);

    @Inject @Default(values = "Ankur Chauhan")
    private String firstName;

    private String value;

    private Page resourcePage;

    private InheritanceValueMap pageProperties;

    private ValueMap properties;

    public void activate() {" ================= ");
        String[] cloudServices = pageProperties.getInherited("cq:cloudserviceconfigs",new String[0]); + " = lenght of array");
        for (String x : cloudServices) {
  "Configuration is = " + x);
        firstName = properties.get("firstName","");;

    public String getValue() {
        return "Hi! "+firstName;
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!

Share it:

Ankur Chauhan

January 18, 2016

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Comment

Related Posts