Expand my Community achievements bar.

SOLVED

How to inject page property into Sling Model using annotations

Avatar

Level 2

Hi everyone!

 

I would like to inject a page property into a Sling Model using annotations but I'm struggling. Like @ValueMapValue but for a page property.

 

Any ideas on how to do it?

Thanks!

1 Accepted Solution

Avatar

Correct answer by
Level 9

Hi @bruno-bradach 

 

Page property is associated with the page and not to a component so I believe you cannot directly use inject or valuemapvalue to read the property.

 

However you can use the below annotations

@ScriptVariable
private ValueMap pageProperties;

@SlingObject
private Page currentPage;

 

which will read the properties and use the get method and retrieve the required property from the page.

 

Hope it helps 

View solution in original post

3 Replies

Avatar

Correct answer by
Level 9

Hi @bruno-bradach 

 

Page property is associated with the page and not to a component so I believe you cannot directly use inject or valuemapvalue to read the property.

 

However you can use the below annotations

@ScriptVariable
private ValueMap pageProperties;

@SlingObject
private Page currentPage;

 

which will read the properties and use the get method and retrieve the required property from the page.

 

Hope it helps 

Avatar

Community Advisor

Hello @bruno-bradach 

 

Sharing a cheat sheet for different annotations used in Sling Models (Including ValueMap).

https://sourcedcode.com/blog/aem/aem-sling-model-injectors-annotations-cheat-sheet-reference-guide

https://techrevel.blog/2017/03/18/sling-model-annotations/

 

It will help with current and later use-cases as well


Aanchal Sikka