{"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 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 Then you can directly inject properties as shown below-<\/p>\n For resolving this issue, \u00a0you need to add one more maven dependency into your project the dependency name is For getting this object in your Sling Model class, you need to inject a field as shown below-<\/p>\n With SlingHttpServletRequest.class <\/strong>as an\u00a0adaptable value.<\/p>\n Yes, you can do that. Here is the syntax-<\/p>\n For getting this object in your Sling Model class, you need to inject a field as shown below-<\/p>\n With SlingHttpServletRequest.class <\/strong>as an\u00a0adaptable value.<\/p>\n Yes, here it is-<\/p>\n I have created a dummy component and that component calls these Sling Model classes. Sightly code snippet is-<\/p>\n For complete working code, I am sharing the Git repository link. Happy Coding!<\/p>\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":"\nQ1. How will I get properties object in Sling Model Class?<\/strong><\/h5>\n
@Model(adaptables = {SlingHttpServletRequest.class})<\/pre>\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
\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
@Inject\r\nprivate InheritanceValueMap pageProperties;<\/pre>\n
Q4. Can I use Resouce.class as well as slingHttpServletRequest.class as adaptable in the same\u00a0<\/strong>class?
\n<\/strong><\/h5>\n@Model(adaptables = {SlingHttpServletRequest.class,Resource.class})<\/pre>\n
Q5. How will I get page object in Sling Model class?<\/strong><\/h5>\n
@Inject\r\nprivate Page resourcePage;<\/pre>\n
Q6. Do you have any working Sling model class example that implements all these properties?
\n<\/strong><\/h5>\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
<div data-sly-use.serviceResolver=\"sling.models.CQDefinedObjectImplModel\">\r\n ${serviceResolver.value}\r\n<\/div><\/pre>\n
\nhttps:\/\/bitbucket.org\/argildx\/accunity-blog-snippets\/src\/master\/<\/a><\/p>\n<\/div>\n<\/div>\nRelated Blogs:<\/h2>\n