Sling model two adapters, adapt problems | Community
Skip to main content
Level 3
November 29, 2018
Solved

Sling model two adapters, adapt problems

  • November 29, 2018
  • 8 replies
  • 9684 views

Hi,

I have a sling model with both SlingHttpServletRequest and Resource as adaptables.

Couple of questions there.

1. I think the default adaptable is request (has the higher priority) since for any inject which depends on resource being the adaptable does not work until i put a Via("resource") there. Is this understanding correct?

2. When I adapt a resource (another model in the same component called from the same HTL file) to the model above, it throws (Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private java.lang.String org.yadhu.core.test.MyComponent.parameter1). Basically it is asking me to make all those Injects optional which depend on request being adaptable. is this the expected behavior? Does this mean that whenever i have a model which has two adapters i need to make everything optional as I could potentially adapt both resource and request? please let me know.

thanks in advance all experts!

Yadhu

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 arunpatidar

In some cases, a different object should be used as the adaptable instead of the original adaptable. This can be done using the @Via annotation. By default, this can be done using a JavaBean property of the adaptable:

@Model(adaptables=SlingHttpServletRequest.class) public interface MyModel // will return request.getResource().getValueMap().get("propertyName", String.class) @Inject @Via("resource") String getPropertyName(); } 

A different strategy can be used to define the adaptable by specifying a type attribute:

@Model(adaptables=Resource.class) public interface MyModel {  // will return resource.getChild("jcr:content").getValueMap().get("propertyName", String.class) @Inject @Via(value = "jcr:content", type = ChildResource.class) String getPropertyName();  }

I am not sure about your second point, can you check below article where there is no optional annotation used

Performing Sling Model Adaptation Using Request and Resource Objects

8 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
November 29, 2018

In some cases, a different object should be used as the adaptable instead of the original adaptable. This can be done using the @Via annotation. By default, this can be done using a JavaBean property of the adaptable:

@Model(adaptables=SlingHttpServletRequest.class) public interface MyModel // will return request.getResource().getValueMap().get("propertyName", String.class) @Inject @Via("resource") String getPropertyName(); } 

A different strategy can be used to define the adaptable by specifying a type attribute:

@Model(adaptables=Resource.class) public interface MyModel {  // will return resource.getChild("jcr:content").getValueMap().get("propertyName", String.class) @Inject @Via(value = "jcr:content", type = ChildResource.class) String getPropertyName();  }

I am not sure about your second point, can you check below article where there is no optional annotation used

Performing Sling Model Adaptation Using Request and Resource Objects

Arun Patidar
Feike_Visser1
Adobe Employee
Adobe Employee
November 29, 2018

"I think the default adaptable is request "

It is really about what is done first, request or resource. Nothing about priority here

Level 3
November 30, 2018

Hey thanks for the info. But does not actually answer my question. does request have a higher priority?

As far the second point goes, i have read the article. does not work that way. I have tested it and the results are consistent, across versions

Level 3
November 30, 2018

Hi Feike,

how do you mean "what is done first"?

Does it mean what you put as first in adaptable list (in annotation)? does not matter... either ways it gives the request the priority...

Would be great if you can explain further. thanks.

Level 3
November 30, 2018

Hi Arun,

One more thing... I have 6.4.. not sure what specification of SLing model is being used there.

type attribute does not seem to work with @Via.

type = ChildResource.class

is this true?

thanks,

Yadhu

Level 3
November 30, 2018

Am i using the correct version of uber jar?

uber-jar-6.2.0-javadoc.jar

Feike_Visser1
Adobe Employee
Adobe Employee
November 30, 2018

here you can find the right version of the uber-jar:  Index of /groups/public/com/adobe/aem/uber-jar

Feike_Visser1
Adobe Employee
Adobe Employee
November 30, 2018