We have two components:
Book Component (sling:resourceSuperType="xyz/components/image")
Book Listing Component
In Book Component Model Implementation, init method we are using Modal Factory to get image model.
"image = modelFactory.getModelFromWrappedRequest(request, request.getResource(), Image.class);"
In Listing component Component Model Implementation, init method based on some filters, we are getting few Book Pages. From these book pages we are getting their respective book details node which we are trying to adapt to Book Model.
Solved! Go to Solution.
Views
Replies
Total Likes
Hello @S__k__Agarwal ,
BookDetails bookDetails = bookDetailsResource.adaptTo(BookDetails.class);
The following code should work. But before that, I will recommend you to make sure the bookDetailsResource has the property,
sling:resourceType=xyz/components/book
Also, the BookDetails model class is adaptable with Resource.
@Model(adaptables = Resource.class,
Hello @Sady_Rifat,
I have done exactly the same thing but the issue is with book image.
In Book Component Model Implementation, init method we are using Modal Factory to get image model.
"image = modelFactory.getModelFromWrappedRequest(request, request.getResource(), Image.class);"
adaptTo is giving the above error in this line.
Thanks & Regards,
Sumit
Thanks for responding,
image = modelFactory.createModel(resource, Image.class);
also, you mentioned image = resource.adaptTo(Image.class);
Both of them are getting null point exceptions.
I am assuming your Annotate modelFactory is not null. It is perfectly initialized.
Since both line doesn't work, it's pretty much sure the problem is in your resource.
Can you please make sure the resource has this?
sling:resourceType=xyz/components/image
And the Image model is adaptable. I also assumed the image is your custom image reference and you followed the Sling Deligation Pattern.
Hello @Sady_Rifat
1. Yes, I even tried image = modelFactory.createModel(resource, Image.class);
2. No, I never mentioned this image = resource.adaptTo(Image.class);
3. Yes, my model factory is not null. ModelFactory org.apache.sling.models.impl.ModelAdapterFactory@28b8df3d
4. my resource is having resource type as below:
Book Component
(sling:resourceType="xyz/components/book")
(sling:resourceSuperType="xyz/components/image")
I tried to reproduce the issue on my local environment. In both the BookList and BookDetails sling model classes, I used the getModelFromWrappedRequest method from the ModelFactory class. It is functioning as expected.
// BookDetails Model init [Book Component]
Image image = modelFactory.getModelFromWrappedRequest(
request, resource, Image.class);
// BookList Model init [Book Listing Component]
BookDetails book = modelFactory.getModelFromWrappedRequest(
request, resource, BookDetails.class);
Additionally, I have pushed the code to a GitHub repository, and you can review the pull request for your reference. https://github.com/MahediSabuj/aem-community/pull/1/files
You can check
@S__k__Agarwal Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies