Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Model annotations for different adaptables

Avatar

Level 6

Hi,

 

 

Annotation	Description
@Model	declares a model class or interface
@Inject	marks a field or method as injectable
@Self	Injects the adaptable itself. If the field type does not match with the adaptable it is tried to adapt the adaptable to the requested type
@Named	declare a name for the injection (otherwise, defaults based on field or method name)
@Optional	marks a field or method injection as optional
@Source	explictly tie an injected field or method to a particular injector (by name). can also be on other annotations.
@PostConstruct	methods to call upon model option creation (only for model classes)
@Via	change the adaptable as the source of the injection
@Default	set default values for a field or method
@ScriptVariable	Injects the script variable defined via Sling Bindings. If name is not set the name is derived from the method/field name
@ValueMapValue	Injects a ValueMap value. If via is not set, it will automatically take resource if the adaptable is the SlingHttpServletRequest. If name is not set the name is derived from the method/field name
@RequestAttribute	Injects a request attribute by name. If name is not set the name is derived from the method/field name
@OSGiService	Injects an OSGi service by type. The filter can be used give an OSGi service filter
@SlingObject	Injects commonly used sling objects if the field matches with the class: request, response, resource resolver, current resource, SlingScriptHelper

 

Is there any specific documentation on which annotation is valid for which adaptable (Resource vs SlingHttpRequest), For example: I could not use @scriptvariable Page currentPage if adaptable is Resource.class. currentPage returns null. 

 

I would need documentation or API which tells which annotations provide what all objects and which annotations are valid for Resource, SlingHttpRequest adaptables.

 

I looked at this documentation but doesnot clearly says what annotations are valid for which adaptables.

https://sling.apache.org/documentation/bundles/models.html

 

Any API classes implementation source code to look at also fine.

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hard to say. It feels somehow intuitive for me, but I understand that this is just me.

 

@ScriptVariable only works in the context where such scripting variables are available and that is only the case when model is adapted from a request. Unfortunately the Sling Model documentation lacks this information, I will add it.

 

Thanks for bringing this up,

Jörg

View solution in original post

6 Replies

Avatar

Community Advisor

HI @sreenu539 

 

Unfortunately, there is no specific document that explains the adaptable w.r.t the annotations. But please look at the text that's mentioned in the same link that you have pasted for the exact usage of the adaptables

Kiran_Vedantam_0-1674690545231.png

 


Hope this helps!


Thanks,
Kiran Vedantam.

Avatar

Correct answer by
Employee Advisor

Hard to say. It feels somehow intuitive for me, but I understand that this is just me.

 

@ScriptVariable only works in the context where such scripting variables are available and that is only the case when model is adapted from a request. Unfortunately the Sling Model documentation lacks this information, I will add it.

 

Thanks for bringing this up,

Jörg