{"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

Q1) Issues while using Sling Models with Sightly in AEM6.0 instance?<\/strong><\/h5>\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

@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

But if you want to inject ResourceResolver or Resource object as shown below –<\/p>\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

 <\/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

Q2) How to resolve this issue?<\/strong><\/h5>\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.
\nLatest versions for these dependencies were 1.2.2 at the time of writing this blog.<\/p>\n

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

Q3) Do I need these packages in AEM6.1?<\/strong><\/h5>\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

Q4) You are talking about AEM6.x versions, what about the older versions of CQ?<\/strong><\/h5>\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

Q5) Where to find all Injectors information supported by any AEM instance?<\/strong><\/h5>\n

You can check this information at-
\nhttp:\/\/<host>:<port>\/system\/console\/status-slingmodels<\/span><\/p>\n

I am also sharing the Git repository link.
\nGit repository link is \u2013<\/p>\n

https:\/\/bitbucket.org\/argildx\/accunity-blog-snippets<\/a><\/p>\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 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":"\nUsing Sling Models with Sightly in AEM - Part2 | Solve Issues Easily<\/title>\n<meta name=\"description\" content=\"Are you facing Issues while using Sling Models with Sightly in AEM6.0 instance? Here is a list of key points to help you fix common issues quickly.\" \/>\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-ii-key-points\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Sling Models with Sightly in AEM - Part2 | Solve Issues Easily\" \/>\n<meta property=\"og:description\" content=\"Are you facing Issues while using Sling Models with Sightly in AEM6.0 instance? Here is a list of key points to help you fix common issues quickly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-ii-key-points\/\" \/>\n<meta property=\"og:site_name\" content=\"Argil DX\" \/>\n<meta property=\"article:published_time\" content=\"2015-12-18T04:20:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-17T06:20:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.argildx.us\/wp-content\/uploads\/2015\/12\/Sling-Models-with-Sightly-Part-\u2013-II-Key-Points-.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-ii-key-points\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/www.argildx.us\/wp-content\/uploads\/2015\/12\/Sling-Models-with-Sightly-Part-\\u2013-II-Key-Points-.jpg\",\"width\":1440,\"height\":542,\"caption\":\"Sling Models with Sightly\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-ii-key-points\/#webpage\",\"url\":\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-ii-key-points\/\",\"name\":\"Using Sling Models with Sightly in AEM - Part2 | Solve Issues Easily\",\"isPartOf\":{\"@id\":\"https:\/\/www.argildx.us\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-ii-key-points\/#primaryimage\"},\"datePublished\":\"2015-12-18T04:20:20+00:00\",\"dateModified\":\"2020-08-17T06:20:49+00:00\",\"author\":{\"@id\":\"https:\/\/www.argildx.us\/#\/schema\/person\/1c5b6f3f2f7218d9acb851588b98551f\"},\"description\":\"Are you facing Issues while using Sling Models with Sightly in AEM6.0 instance? Here is a list of key points to help you fix common issues quickly.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.argildx.us\/technology\/sling-models-sightly-part-ii-key-points\/\"]}]},{\"@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\/3915"}],"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=3915"}],"version-history":[{"count":0,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/posts\/3915\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/media\/6948"}],"wp:attachment":[{"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/media?parent=3915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/categories?post=3915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/tags?post=3915"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/yst_prominent_words?post=3915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}