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
Solved! Go to Solution.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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
Views
Replies
Total Likes
"I think the default adaptable is request "
It is really about what is done first, request or resource. Nothing about priority here
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
Am i using the correct version of uber jar?
uber-jar-6.2.0-javadoc.jar
Views
Replies
Total Likes
here you can find the right version of the uber-jar: Index of /groups/public/com/adobe/aem/uber-jar
Views
Replies
Total Likes
here you see the source code, first it tries via request and then resource:
Views
Replies
Total Likes