Sling model two adapters, adapt problems

Avatar

Avatar

Aryan12345

Avatar

Aryan12345

Aryan12345

29-11-2018

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

3.1K

Likes

1.2K

Correct Reply

883

Avatar

Arun_Patidar

MVP

Total Posts

3.1K

Likes

1.2K

Correct Reply

883
Arun_Patidar
MVP

29-11-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

Answers (7)

Answers (7)

Avatar

Avatar

Feike_Visser1

Employee

Avatar

Feike_Visser1

Employee

Feike_Visser1
Employee

29-11-2018

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 · apa...

Avatar

Avatar

Feike_Visser1

Employee

Avatar

Feike_Visser1

Employee

Feike_Visser1
Employee

29-11-2018

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

Avatar

Avatar

Aryan12345

Avatar

Aryan12345

Aryan12345

29-11-2018

Am i using the correct version of uber jar?

uber-jar-6.2.0-javadoc.jar

Avatar

Avatar

Aryan12345

Avatar

Aryan12345

Aryan12345

29-11-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?

Via error2.jpg

Via error_edited.jpg

thanks,

Yadhu

Avatar

Avatar

Aryan12345

Avatar

Aryan12345

Aryan12345

29-11-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.

Avatar

Avatar

Aryan12345

Avatar

Aryan12345

Aryan12345

29-11-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

Avatar

Avatar

Feike_Visser1

Employee

Avatar

Feike_Visser1

Employee

Feike_Visser1
Employee

29-11-2018

"I think the default adaptable is request "

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