{"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 <\/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> If you don\u2019t add these entries, then maven will not add 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 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. Create the component as shown below-<\/p>\n My dialog properties are as shown below:\u00a0–<\/p>\n <\/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- Open the dialog of this component and add any entry (For example “Ankur Chauhan”) you will see the desired output as- 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. https:\/\/bitbucket.org\/argildx\/accunity-blog-snippets\/src\/master\/<\/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 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":"\n
\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
\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>\npackage 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
\n@PostConstructor annotation defines that this method will run after injecting all field (having @Inject annotation) from the resource.<\/p>\n
\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<\/a><\/p>\n
\n<\/a><\/p>\nQ2). 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
\nGit repository link is \u2013<\/p>\nRelated Blogs:<\/h2>\n