{"id":3918,"date":"2015-12-16T06:03:53","date_gmt":"2015-12-16T00:33:53","guid":{"rendered":"\/?p=3918"},"modified":"2020-08-17T11:50:41","modified_gmt":"2020-08-17T06:20:41","slug":"sling-model-sightly-part","status":"publish","type":"post","link":"https:\/\/www.argildx.us\/technology\/sling-model-sightly-part\/","title":{"rendered":"How to use Sling Models with Sightly – Part1"},"content":{"rendered":"

In this post, I will explain, how to use sling models with Sightly in AEM6.x?<\/strong><\/em><\/p>\n

For doing this, I have created a project using maven archetype for AEM6. If you want to use any existing project, then you need to check two things-<\/p>\n

1. Dependency for Sling models in your project\u2019s pom.xml file.<\/strong> You can find this maven dependency in you AEM instance package finder tab as shown in fig-<\/p>\n

\"1.1\"<\/a><\/p>\n

Now search for this dependency in your Maven project\u00a0parent pom.xml file. If it\u2019s already there then it\u2019s fine else add this dependency into your project.<\/p>\n

2. In whatever java packages, you want to add your Sling Model classes, add these java packages information into your maven-bundle-plugin.<\/strong>
\nFor example, I am using two java package for adding my Sling Model classes, these packages are-
\nsling.models<\/strong> and com.blog.sling.models,<\/strong>\u00a0so I have to place these package information into my maven-bundle-plugin<\/strong>, as shown below-<\/p>\n

<plugin>\r\n\u00a0\u00a0\u00a0 <groupId>org.apache.felix<\/groupId>\r\n\u00a0\u00a0\u00a0 <artifactId>maven-bundle-plugin<\/artifactId>\r\n\u00a0\u00a0\u00a0 <extensions>true<\/extensions>\r\n\u00a0\u00a0\u00a0 <configuration>\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <instructions>\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <Sling-Model-Packages>\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sling.models,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0com.blog.sling.models\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/Sling-Model-Packages>\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <Bundle-Category>sling-model-demo<\/Bundle-Category>\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/instructions>\r\n\u00a0\u00a0\u00a0 <\/configuration>\r\n<\/plugin>\r\n<\/pre>\n
Q1). What will happen, If I don\u2019t add these entries?<\/strong><\/h5>\n

If you don\u2019t add these entries, then maven will not add
\n“Sling-Model-Packages: sling.models, com.blog.sling.models”<\/strong>
\nheader entry in your bundle Manifest file<\/strong>. So that these classes will not behave as sling models and will work\u00a0as simple java classes and, If you try to run your code without this entry, then you will not get desired output as well as there will be no error message in error.log<\/strong> file.<\/p>\n

In this post, I will create a very basic example where I will get a name from the component dialog and print that value using Sling Model. So Let\u2019s start-<\/p>\n

Create a Sling model class.<\/p>\n

package sling.models;\r\n\r\nimport org.apache.sling.api.resource.Resource;\r\nimport org.apache.sling.models.annotations.Model;\r\nimport org.apache.sling.models.annotations.Optional;\r\n\r\nimport javax.annotation.PostConstruct;\r\nimport javax.inject.Inject;\r\n\r\n@Model(adaptables = Resource.class)\r\npublic class ResourceModel {\r\n\r\n    @Inject\r\n    private String firstName;\r\n    private String value;\r\n\r\n    @PostConstruct\r\n    public void activate() {\r\n        value = \"Hi! \" + firstName;\r\n    }\r\n\r\n    public String getValue() {\r\n        return value;\r\n    }\r\n}<\/pre>\n

Here I am using @Inject annotation on “firstName<\/strong>” property, it means\u00a0that this property will be looked up from the resource (after first adapting it to a ValueMap) and it is injected.
\n@PostConstructor annotation defines that this method will run after injecting all field (having @Inject annotation) from the resource.<\/p>\n

Create the component as shown below-<\/p>\n

\"1.2\"
\n<\/a>In slingModel.html<\/strong> file add these lines.<\/p>\n

<h4> Sling Model Examples <\/h4>\r\n<div data-sly-test=\"${properties.firstName}\" data-sly-use.slingModel=\"sling.models.ResourceModel\">\r\n     ${slingModel.value}\r\n<\/div>\r\n<\/strong><\/pre>\n
\n

My dialog properties are as shown below:\u00a0–<\/p>\n

\"Dialogue<\/a><\/p>\n<\/div>\n

 <\/p>\n

 <\/p>\n

Build your maven project and drop this component on your page. You will see a screen as-
\n
\"1.3\"<\/a><\/p>\n

Open the dialog of this component and add any entry (For example “Ankur Chauhan”) you will see the desired output as-
\n
\"1.5\"<\/a><\/p>\n

Q2). Why are we creating Sling Model class for this example even we can directly use ${properties.firstName} in our slingModel.html file?<\/strong><\/h5>\n

You are right, We can do that or you can say, we must do that. But as I already mention that, in this post I am going to show you a very basic use of Sling Models. So I think this is the simplest example for this post. You will see must better examples in my coming blogs.<\/p>\n

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

https:\/\/bitbucket.org\/argildx\/accunity-blog-snippets\/src\/master\/<\/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 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

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

In this post, I will explain, how to use sling models with Sightly in AEM6.x? For doing this, I have created a project using maven archetype for AEM6. If you want to use any existing project, then you need to check two things- 1. Dependency for Sling models in your project\u2019s pom.xml file. You can … Read more<\/a><\/p>\n","protected":false},"author":29,"featured_media":6947,"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":[82,46,43],"yst_prominent_words":[1306,1305,830,823,1304,1608,1298,878,1254,1287,1607,1303,831,835,876,869,1248,1278,726,1307],"acf":[],"yoast_head":"\nHow to use Sling Models with Sightly in AEM - Part1 | Argil DX<\/title>\n<meta name=\"description\" content=\"Learn how to use sling models with sightly in AEM. The process is explained through a project created using maven archetype for AEM6.0\" \/>\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-model-sightly-part\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use Sling Models with Sightly in AEM - Part1 | Argil DX\" \/>\n<meta property=\"og:description\" content=\"Learn how to use sling models with sightly in AEM. The process is explained through a project created using maven archetype for AEM6.0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.argildx.us\/technology\/sling-model-sightly-part\/\" \/>\n<meta property=\"og:site_name\" content=\"Argil DX\" \/>\n<meta property=\"article:published_time\" content=\"2015-12-16T00:33:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-17T06:20:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.argildx.us\/wp-content\/uploads\/2015\/12\/Sling-Model-with-Sightly-Part-\u2013-I.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-model-sightly-part\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/www.argildx.us\/wp-content\/uploads\/2015\/12\/Sling-Model-with-Sightly-Part-\\u2013-I.jpg\",\"width\":1440,\"height\":542,\"caption\":\"Sling-Model-with-Sightly\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.argildx.us\/technology\/sling-model-sightly-part\/#webpage\",\"url\":\"https:\/\/www.argildx.us\/technology\/sling-model-sightly-part\/\",\"name\":\"How to use Sling Models with Sightly in AEM - Part1 | Argil DX\",\"isPartOf\":{\"@id\":\"https:\/\/www.argildx.us\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.argildx.us\/technology\/sling-model-sightly-part\/#primaryimage\"},\"datePublished\":\"2015-12-16T00:33:53+00:00\",\"dateModified\":\"2020-08-17T06:20:41+00:00\",\"author\":{\"@id\":\"https:\/\/www.argildx.us\/#\/schema\/person\/1c5b6f3f2f7218d9acb851588b98551f\"},\"description\":\"Learn how to use sling models with sightly in AEM. The process is explained through a project created using maven archetype for AEM6.0\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.argildx.us\/technology\/sling-model-sightly-part\/\"]}]},{\"@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\/3918"}],"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=3918"}],"version-history":[{"count":0,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/posts\/3918\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/media\/6947"}],"wp:attachment":[{"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/media?parent=3918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/categories?post=3918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/tags?post=3918"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.argildx.us\/wp-json\/wp\/v2\/yst_prominent_words?post=3918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}