May 26, 2020
Estimated Post Reading Time ~

Get Object in Sling Model Class? Sling Models with Sightly Part – V (Key Annotations – III)

Learn how to access cq:defined Object in Sling Model classes. Before answering this question, I want to make sure that you guys are using the latest Sling Model dependency and AEM version 6.x. So let’s start-

Q1. How will I get the properties to 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
cq–commens and its 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 the 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 a page object in the 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 the Sling Model class?
I have created a dummy component and that component calls these Sling Model classes. Sightly code snippet is-

<div data-sly-use.serviceResolver="sling.models.CQDefinedObjectImplModel">
    ${serviceResolver.value}
</div>

For complete working code, I am sharing the Git repository link.
https://bitbucket.org/argildx/accunity-blog-snippets/src/master/


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.