{"id":3915,"date":"2015-12-18T09:50:20","date_gmt":"2015-12-18T04:20:20","guid":{"rendered":"\/?p=3915"},"modified":"2020-08-17T11:50:49","modified_gmt":"2020-08-17T06:20:49","slug":"sling-models-sightly-part-ii-key-points","status":"publish","type":"post","link":"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-ii-key-points\/","title":{"rendered":"How to use Sling Models with Sightly – Part2"},"content":{"rendered":"
Let’s discuss some key points to make working in AEM using Sling Models with Sightly easier.<\/p>\n
If you are using AEM6.0 then it provides org.apache.sling.models.api<\/strong>\u00a0bundle with version 1.0.0<\/strong> with very limited functionalities. As an example, you can inject some properties which exist on the resource and also you can use some basic annotations. See snippet examples below:<\/p>\n But if you want to inject ResourceResolver or Resource object as shown below –<\/p>\n <\/p>\n then this code will not run.<\/strong> Because Injectors for these kinds of Objects are not supported in Sling Model API version 1.0.0. In this case, you will get a warning that \u201cnot able to inject these properties\u201d <\/strong>or\u00a0you will get a null or NullPointerException.<\/p>\n Another point is “@Self”<\/strong> annotation will not work in AEM6.0. as its injector is also not available in Sling Model API version 1.0.0.<\/p>\n For resolving this issue just go to the maven repository and download the latest bundles for org.apache.sling.models.api<\/strong> and org.apache.sling.models.impl<\/strong> bundles. Note :- \u00a0Both of these dependencies are mandatory.<\/strong><\/em><\/p>\n Now you can install these bundles in two different ways.<\/p>\n 1)<\/strong>\u00a0Directly install these bundles using \/system\/console\/bundles<\/strong><\/em> tab.<\/p>\n 2)<\/strong>\u00a0Place these bundles under \/apps\/<project>\/install<\/strong><\/em> folder in your maven project so that whenever you build your project these bundles are automatically installed in your AEM instance.<\/p>\n AEM6.1 provides 1.1.0 version for these APIs, that support all annotations, but it\u2019s always good to go with the latest APIs version. So I think you should keep these latest versions in your project for AEM6.1 instance. If you keep these JAR files in your project, you wont need to worry about the issue, that\u00a0your code will run or not in any of the AEM6.x instances.<\/p>\n I am talking about these two versions because Sightly is only supported from AEM6.0 version but if you are working with CQ5.x version and want to use Sling Modes in your JSP files then you can follow the same steps by placing these API bundles into your maven project install directory and your code will work perfectly fine.<\/p>\n You can check this information at- I am also sharing the Git repository link. https:\/\/bitbucket.org\/argildx\/accunity-blog-snippets<\/a><\/p>\n 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 III (Key Annotations \u2013 I)<\/a><\/p>\n Sling Models with Sightly \u2013 Part IV (Key Annotations II) <\/a><\/p>\n Sling Models with Sightly Part \u2013 V (Key Annotations \u2013 III)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":" Let’s discuss some key points to make working in AEM using Sling Models with Sightly easier. Q1) Issues while using Sling Models with Sightly in AEM6.0 instance? If you are using AEM6.0 then it provides org.apache.sling.models.api\u00a0bundle with version 1.0.0 with very limited functionalities. As an example, you can inject some properties which exist on the … Read more<\/a><\/p>\n","protected":false},"author":29,"featured_media":6948,"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":[44,45],"yst_prominent_words":[1297,1293,823,1295,1608,1298,1296,1292,1290,1607,835,876,1291,1289,1288,1278,726,1699,1698,1294],"acf":[],"yoast_head":"\n@Model(adaptables=Resource.class)\r\npublic class SampleSlingModel {\r\n\r\n @Inject @Named(\"firstName\")\r\n private String title;\r\n\r\n @PostConstruct\r\n private void calculateTotalMatches() { \r\n System.out.println(\"Hi! \" + title);\r\n }\r\n}<\/pre>\n
@Model(adaptables = Resource.class)\r\npublic class ResourceModel{\r\n\r\n @Inject @Default(values = \"Ankur Chauhan\")\r\n private String firstName;\r\n\r\n @Self\r\n private Resource resource;\r\n\r\n @Inject\r\n private ResourceResolver resourceResolver;\r\n\r\n @PostConstruct\r\n public void activate() { \r\n System.out.println(resource);\r\n }\r\n}<\/pre>\n
Q2) How to resolve this issue?<\/strong><\/h5>\n
\nLatest versions for these dependencies were 1.2.2 at the time of writing this blog.<\/p>\nQ3) Do I need these packages in AEM6.1?<\/strong><\/h5>\n
Q4) You are talking about AEM6.x versions, what about the older versions of CQ?<\/strong><\/h5>\n
Q5) Where to find all Injectors information supported by any AEM instance?<\/strong><\/h5>\n
\nhttp:\/\/<host>:<port>\/system\/console\/status-slingmodels<\/span><\/p>\n
\nGit repository link is \u2013<\/p>\nRelated Blogs:<\/h2>\n