Sling model two adapters, adapt problems | Community
Skip to main content
Level 3
Nov 29, 2018 1:45 PM UTC
Solved

Sling model two adapters, adapt problems

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.
arunpatidar
arunpatidarAccepted solution
New Participant
Nov 29, 2018 2:39 PM UTC

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

8 replies

Feike_Visser1
Employee
Nov 30, 2018 7:41 AM UTC

here you see the source code, first it tries via request and then resource:

sling-org-apache-sling-scripting-sightly-models-provider/SlingModelsUseProvider.java at master · apache/sling-org-apache…

Translate
Feike_Visser1
Employee
Nov 30, 2018 7:35 AM UTC

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

Translate
Level 3
Nov 30, 2018 7:27 AM UTC

Am i using the correct version of uber jar?

uber-jar-6.2.0-javadoc.jar

Translate
Level 3
Nov 30, 2018 7:21 AM UTC

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

Translate
Level 3
Nov 30, 2018 3:03 AM UTC

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.

Translate
Level 3
Nov 30, 2018 3:01 AM UTC

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

Translate
Feike_Visser1
Employee
Nov 29, 2018 3:12 PM UTC

"I think the default adaptable is request "

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

Translate
arunpatidar
arunpatidarAccepted solution
New Participant
Nov 29, 2018 2:39 PM UTC

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

Translate
Arun Patidar