Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to adaptTo a myComponent in Java

Avatar

Level 6

I have a class MyList

@Model(adaptables = SlingHttpServletRequest.class,

       adapters = MyList.class,

       resourceType = MyListImpl.RESOURCE_TYPE)

in Sightly, you can do this

<sling:adaptTo adaptable="${slingRequest}" adaptTo="MyList" var="m"/>

How can I do the same in Java code?

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Employee

MyList ml = request.adaptTo(MyList.class);

View solution in original post

2 Replies

Avatar

Correct answer by
Employee

MyList ml = request.adaptTo(MyList.class);

Avatar

Level 6

Thanks.

I would like to add more detail and hopefully I can get an answer for that.

I have a path pointing to a node which has the sling:resourceType is myList component. I wrote MyList.java

using  @Model(adaptables = SlingHttpServletRequest.class, adapters = MyList.class,resourceType = MyListImpl.RESOURCE_TYPE).

Is there a way I can adaptTo(MyList.class) and get myList object.

If MyList.java implements using @Model(adaptables=Resource), then I can do this

Resource myListResource =  resourceResolver.getResource(path);

MyList ml = MyListResource.adaptTo(MyList.class);

Just don't know how to get MyList object when implemented using adaptable is SlingHttpServletRequest.class in Java?

Thanks.