Blogs

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-

@Inject
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/CQDefinedObjectImplModel.java:[3,34] package com.day.cq.commons.inherit does not exist
[ERROR] /D:/accunity/blog/bundle/src/main/java/sling/models/CQDefinedObjectImplModel.java:[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/CQDefinedObjectImplModel.java:[39,33] cannot access com.day.cq.commons.LabeledResource
[ERROR] class file for com.day.cq.commons.LabeledResource not found
[ERROR] -> [Help 1]
[ERROR]
[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]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[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-

<dependency>
   <groupId>com.day.cq</groupId>
   <artifactId>cq-commons</artifactId>
   <version>5.5.0</version>
   <scope>provided</scope>
</dependency>
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-

@Inject
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-

@Inject
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;

    @Inject
    private Page resourcePage;

    @Inject
    private InheritanceValueMap pageProperties;

    @Inject
    private ValueMap properties;

    @PostConstruct
    public void activate() {

        logger.info(resourcePage.getPath()+" ================= ");
        String[] cloudServices = pageProperties.getInherited("cq:cloudserviceconfigs",new String[0]);
        logger.info(cloudServices.length + " = lenght of array");
        for (String x : cloudServices) {
            logger.info("Configuration is = " + x);
        }
        firstName = properties.get("firstName","");
        logger.info(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!

Related Blogs:

New Features in AEM 6.1 Sightly

Understanding Sling Models in AEM

Sling Model with Sightly Part – I

Sling Models with Sightly Part – II ( Key Points )

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

Sling Models with Sightly – Part IV (Key Annotations II)

Share it:

Ankur Chauhan

January 18, 2016

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Comment

Related Posts