I’m very impressed with Sling Models, they provide easy and very effective binding mechanism. The combination of Sightly and Sling Models is simply superb. It has made the developers life very easy. Here is a simple example of retrieving the multifield values using Sling Models and Sightly.
The screen shot below shows the node structure of the multifield. The multifield has 2 field, a textfield and a pathfield. “item” nodes store the value of each row of the multifield. “headermenu” is the component node and “menuitems” is the multifield node which holds the item node.
Now the logic to retrieve the multifield values. First the Model class.
Model Class:
Sightly Code
When you use “data-sly-use” and assign it to Model class, it will pass the current resource object if I adapt my Model class to Resource. I can inject properties and the nodes under the current resource just by Injecting the variable. Please make sure that the property names and the node names should match exactly with the variables of the Model.
In the above model class, I’m just injecting menuitems node and in the sightly class just retrieving the nodes under it via listChildren method. I did not use even a single get statement in my code.
More information about Sling Models: https://sling.apache.org/documentation/bundles/models.html
Don’t forget to add your package which contains the Model class to your Bundle pom. Refer screen shot below:




Leave a reply to Ankur Cancel reply