May 26, 2020
Estimated Post Reading Time ~

How to use Sling Models with Sightly – Part2

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 bundle with version 1.0.0 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:

@Model(adaptables=Resource.class)
public class SampleSlingModel {

@Inject @Named("firstName")
private String title;

@PostConstruct
private void calculateTotalMatches() {
System.out.println("Hi! " + title);
}
}
But if you want to inject ResourceResolver or Resource object as shown below –

@Model(adaptables = Resource.class)
public class ResourceModel{

@Inject @Default(values = "Ankur Chauhan")
private String firstName;

@Self
private Resource resource;

@Inject
private ResourceResolver resourceResolver;

@PostConstruct
public void activate() {
System.out.println(resource);
}
}


then this code will not run. 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 “not able to inject these properties” or you will get a null or NullPointerException.

Another point is “@Self” annotation will not work in AEM6.0. as its injector is also not available in Sling Model API version 1.0.0.

Q2) How to resolve this issue?
For resolving this issue just go to the maven repository and download the latest bundles for org.apache.sling.models.api and org.apache.sling.models.impl bundles.
The latest versions for these dependencies were 1.2.2 at the time of writing this blog.

Note: Both of these dependencies are mandatory.

Now you can install these bundles in two different ways.
1) Directly install these bundles using /system/console/bundles tab.
2) Place these bundles under /apps/<project>/install folder in your maven project so that whenever you build your project these bundles are automatically installed in your AEM instance.

Q3) Do I need these packages in AEM6.1?
AEM6.1 provides a 1.1.0 version for these APIs, that support all annotations, but it’s always good to go with the latest APIs version. So I think you should keep these latest versions in your project for the AEM6.1 instance. If you keep these JAR files in your project, you won't need to worry about the issue, that your code will run or not in any of the AEM6.x instances.

Q4) You are talking about AEM6.x versions, what about the older versions of CQ?
I am talking about these two versions because Sightly is only supported from the AEM6.0 version but if you are working with the 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.

Q5) Where to find all Injectors information supported by any AEM instance?
You can check this information at-
http://<host>:<port>/system/console/status-slingmodels

I am also sharing the Git repository link.
Git repository link is: https://bitbucket.org/argildx/accunity-blog-snippets


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.