How to adaptTo a myComponent in Java | Community
Skip to main content
August 22, 2017
Solved

How to adaptTo a myComponent in Java

  • August 22, 2017
  • 2 replies
  • 1713 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Feike_Visser1

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

2 replies

Feike_Visser1
Adobe Employee
Feike_Visser1Adobe EmployeeAccepted solution
Adobe Employee
August 23, 2017

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

BigT168Author
August 24, 2017

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.