Expand my Community achievements bar.

SOLVED

Sling model two adapters, adapt problems

Avatar

Level 3

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

8 Replies

Avatar

Correct answer by
Community Advisor

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

Avatar

Employee

"I think the default adaptable is request "

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

Avatar

Level 3

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

Level 3

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

Level 3

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

Level 3

Am i using the correct version of uber jar?

uber-jar-6.2.0-javadoc.jar