I'm working with sling models for a project in AEM. I have my sling model setup to inject values to variables. I can setup getters to retrieve properties from the adapted resource as long as the properties have names that don't break java syntax. (For example: title)
My problem is that there are properties I need that break java syntax (for instance jcr:title,etc). I know for the get servlet for image servlet uses underscores in place of periods (file named img.GET.java, class named img_GET) and was curious if there was some character(s) I can use that will be valid java syntax that the injection strategy recognizes to replace with colons.
I know that I can retrieve these properties in other ways but I was wondering if there is a way to retrieve them using injection to keep my code dry.
Since 1.1. 0 version of Sling Models you can use @Self annotation to inject models which can be adapted from current adaptable. In this case from Resource of ModelA . Injects the adaptable object itself (if the class of the field matches or is a supertype).
The adaptables property determines what source objects can be adapted to the Sling model implementation. Typically this is Resource and/or SlingHttpServletRequest , meaning a Sling resource or Sling request object respectively.
Check out the following blog post:
http://labs.sixdimensions.com/blog/2014-11-21/sling-models-for-fun-and-profit/
In this, Dan provides an example of injecting (ironically enough) the jcr:title property of a Resource. You can simply use the @Named annotation and give it the name of the property you wish to inject.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With