{"id":3904,"date":"2016-01-18T07:48:28","date_gmt":"2016-01-18T02:18:28","guid":{"rendered":"\/?p=3904"},"modified":"2020-08-17T11:49:06","modified_gmt":"2020-08-17T06:19:06","slug":"sling-models-sightly-part-v-key-annotations-iii","status":"publish","type":"post","link":"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-v-key-annotations-iii\/","title":{"rendered":"Get Object in Sling Model Class? Sling Models with Sightly Part – V (Key Annotations – III)"},"content":{"rendered":"

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

Q1. How will I get properties object in Sling Model Class?<\/strong><\/h5>\n

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.<\/p>\n

First use adaptable as SlingHttpServletRequest.class<\/strong> as shown below-<\/p>\n

@Model(adaptables = {SlingHttpServletRequest.class})<\/pre>\n

Then you can directly inject properties as shown below-<\/p>\n

@Inject\r\nprivate ValueMap properties;<\/pre>\n
Q2. During the build time, I am getting a Maven build error as shown below-<\/strong><\/h5>\n
 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project blog-bundle: Compilation failure: Compilation failure:\r\n[ERROR] \/D:\/accunity\/blog\/bundle\/src\/main\/java\/sling\/models\/CQDefinedObjectImplModel.java:[3,34] package com.day.cq.commons.inherit does not exist\r\n[ERROR] \/D:\/accunity\/blog\/bundle\/src\/main\/java\/sling\/models\/CQDefinedObjectImplModel.java:[31,13] cannot find symbol\r\n[ERROR] symbol:   class InheritanceValueMap\r\n[ERROR] location: class sling.models.CQDefinedObjectImplModel\r\n[ERROR] \/D:\/accunity\/blog\/bundle\/src\/main\/java\/sling\/models\/CQDefinedObjectImplModel.java:[39,33] cannot access com.day.cq.commons.LabeledResource\r\n[ERROR] class file for com.day.cq.commons.LabeledResource not found\r\n[ERROR] -> [Help 1]\r\n[ERROR]\r\n[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.\r\n[ERROR] Re-run Maven using the -X switch to enable full debug logging.\r\n[ERROR]\r\n[ERROR] For more information about the errors and possible solutions, please read the following articles:\r\n[ERROR] [Help 1] http:\/\/cwiki.apache.org\/confluence\/display\/MAVEN\/MojoFailureException\r\n[ERROR]\r\n[ERROR] After correcting the problems, you can resume the build with the command\r\n[ERROR]   mvn <goals> -rf :blog-bundle<\/pre>\n
How to resolve this issue?<\/strong><\/h5>\n

For resolving this issue, \u00a0you need to add one more maven dependency into your project the dependency name is
\ncq–<\/strong>commens\u00a0and it’s Maven dependency is-<\/p>\n

<dependency>\r\n   <groupId>com.day.cq<\/groupId>\r\n   <artifactId>cq-commons<\/artifactId>\r\n   <version>5.5.0<\/version>\r\n   <scope>provided<\/scope>\r\n<\/dependency><\/pre>\n
Q3. How will I get inherited page properties object in Sling Model class?<\/strong><\/h5>\n

For getting this object in your Sling Model class, you need to inject a field as shown below-<\/p>\n

@Inject\r\nprivate InheritanceValueMap pageProperties;<\/pre>\n

With SlingHttpServletRequest.class <\/strong>as an\u00a0adaptable value.<\/p>\n

Q4. Can I use Resouce.class as well as slingHttpServletRequest.class as adaptable in the same\u00a0<\/strong>class?
\n<\/strong><\/h5>\n

Yes, you can do that. Here is the syntax-<\/p>\n

@Model(adaptables = {SlingHttpServletRequest.class,Resource.class})<\/pre>\n
Q5. How will I get page object in Sling Model class?<\/strong><\/h5>\n

For getting this object in your Sling Model class, you need to inject a field as shown below-<\/p>\n

@Inject\r\nprivate Page resourcePage;<\/pre>\n

With SlingHttpServletRequest.class <\/strong>as an\u00a0adaptable value.<\/p>\n

Q6. Do you have any working Sling model class example that implements all these properties?
\n<\/strong><\/h5>\n

Yes, here it is-<\/p>\n

@Model(adaptables = {SlingHttpServletRequest.class,Resource.class})\r\npublic class CQDefinedObjectImplModel {\r\n\r\n    private Logger logger = LoggerFactory.getLogger(CQDefinedObjectImplModel.class);\r\n\r\n    @Inject @Default(values = \"Ankur Chauhan\")\r\n    private String firstName;\r\n\r\n    private String value;\r\n\r\n    @Inject\r\n    private Page resourcePage;\r\n\r\n    @Inject\r\n    private InheritanceValueMap pageProperties;\r\n\r\n    @Inject\r\n    private ValueMap properties;\r\n\r\n    @PostConstruct\r\n    public void activate() {\r\n\r\n        logger.info(resourcePage.getPath()+\" ================= \");\r\n        String[] cloudServices = pageProperties.getInherited(\"cq:cloudserviceconfigs\",new String[0]);\r\n        logger.info(cloudServices.length + \" = lenght of array\");\r\n        for (String x : cloudServices) {\r\n            logger.info(\"Configuration is = \" + x);\r\n        }\r\n        firstName = properties.get(\"firstName\",\"\");\r\n        logger.info(firstName);\r\n    }\r\n\r\n    public String getValue() {\r\n        return \"Hi! \"+firstName;\r\n    }\r\n}<\/pre>\n
Q7. How am I testing these annotations in Sling Model class?<\/strong><\/h5>\n

I have created a dummy component and that component calls these Sling Model classes. Sightly code snippet is-<\/p>\n

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

For complete working code, I am sharing the Git repository link.
\nhttps:\/\/bitbucket.org\/argildx\/accunity-blog-snippets\/src\/master\/<\/a><\/p>\n<\/div>\n<\/div>\n

Happy Coding!<\/p>\n

Related Blogs:<\/h2>\n

New Features in AEM 6.1 Sightly <\/a><\/p>\n

Understanding Sling Models in AEM<\/a><\/p>\n

Sling Model with Sightly Part \u2013 I <\/a><\/p>\n

Sling Models with Sightly Part \u2013 II ( Key Points )<\/a><\/p>\n

Sling Models with Sightly Part \u2013 III (Key Annotations \u2013 I)<\/a><\/p>\n

Sling Models with Sightly \u2013 Part IV (Key Annotations II) <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

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 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 … Read more<\/a><\/p>\n","protected":false},"author":29,"featured_media":6946,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","content-type":"","footnotes":""},"categories":[66],"tags":[1746,39,40],"yst_prominent_words":[1252,1253,878,1254,1607,1246,1242,1241,1249,1244,1251,1247,1243,876,869,1248,1278,726,1250,1245],"acf":[],"yoast_head":"\nHow to Get cq:defined Object in Sling Model Class | Argil DX<\/title>\n<meta name=\"description\" content=\"Learn to get properties object in Sling model class in this part of Sling Models with sightly series, as well as solutions to errors you might face.\" \/>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-v-key-annotations-iii\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Get cq:defined Object in Sling Model Class | Argil DX\" \/>\n<meta property=\"og:description\" content=\"Learn to get properties object in Sling model class in this part of Sling Models with sightly series, as well as solutions to errors you might face.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-v-key-annotations-iii\/\" \/>\n<meta property=\"og:site_name\" content=\"Argil DX\" \/>\n<meta property=\"article:published_time\" content=\"2016-01-18T02:18:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-17T06:19:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.argildx.us\/wp-content\/uploads\/2016\/01\/Sling-Models-with-Sightly-Part-\u2013-V-Key-Annotations-\u2013-III.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1440\" \/>\n\t<meta property=\"og:image:height\" content=\"542\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.argildx.us\/#website\",\"url\":\"https:\/\/www.argildx.us\/\",\"name\":\"Argil DX\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.argildx.us\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-v-key-annotations-iii\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/www.argildx.us\/wp-content\/uploads\/2016\/01\/Sling-Models-with-Sightly-Part-\\u2013-V-Key-Annotations-\\u2013-III.jpg\",\"width\":1440,\"height\":542},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-v-key-annotations-iii\/#webpage\",\"url\":\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-v-key-annotations-iii\/\",\"name\":\"How to Get cq:defined Object in Sling Model Class | Argil DX\",\"isPartOf\":{\"@id\":\"https:\/\/www.argildx.us\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-v-key-annotations-iii\/#primaryimage\"},\"datePublished\":\"2016-01-18T02:18:28+00:00\",\"dateModified\":\"2020-08-17T06:19:06+00:00\",\"author\":{\"@id\":\"https:\/\/www.argildx.us\/#\/schema\/person\/1c5b6f3f2f7218d9acb851588b98551f\"},\"description\":\"Learn to get properties object in Sling model class in this part of Sling Models with sightly series, as well as solutions to errors you might face.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-v-key-annotations-iii\/\"]}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/www.argildx.us\/#\/schema\/person\/1c5b6f3f2f7218d9acb851588b98551f\",\"name\":\"Argil DX Media\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.argildx.us\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0ccbc04705942d1269cdf9f789e58484?s=96&d=mm&r=g\",\"caption\":\"Argil DX Media\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","_links":{"self":[{"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/posts\/3904"}],"collection":[{"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/comments?post=3904"}],"version-history":[{"count":0,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/posts\/3904\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/media\/6946"}],"wp:attachment":[{"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/media?parent=3904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/categories?post=3904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/tags?post=3904"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/yst_prominent_words?post=3904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}