$properties['my-field-name-here']: How do I access this in Java? | Community
Skip to main content
jayv25585659
Level 8
January 5, 2024
Solved

$properties['my-field-name-here']: How do I access this in Java?

  • January 5, 2024
  • 3 replies
  • 721 views

So this works ok in HTL/sightly

 

<h3>${properties['my-field-name-here'] @2941342='text'}</h3>

 

But how do I access this value in Java?

 

I have this code in my class but I cannot see "my-field-name-here" in the list. Thoughts? Thanks!

ValueMap test = resource.getValueMap()
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by aanchal-sikka

@jayv25585659 

 

If its a Sling Model, then use ValueMapValue annotation

@ValueMapValue private String mailAddress;

 Details here: https://techrevelhub.wordpress.com/2017/03/18/sling-model-annotations/

 

If its a Service/Servlet, use

resource.getValueMap().get('my-field-name-here', String.class);

Here Resource needs to be the exact node where property exists. Like jcr:content 

 

This link has few examples for various types of properties: https://www.tabnine.com/code/java/methods/org.apache.sling.api.resource.ValueMap/get 

3 replies

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
January 5, 2024

@jayv25585659 

 

If its a Sling Model, then use ValueMapValue annotation

@ValueMapValue private String mailAddress;

 Details here: https://techrevelhub.wordpress.com/2017/03/18/sling-model-annotations/

 

If its a Service/Servlet, use

resource.getValueMap().get('my-field-name-here', String.class);

Here Resource needs to be the exact node where property exists. Like jcr:content 

 

This link has few examples for various types of properties: https://www.tabnine.com/code/java/methods/org.apache.sling.api.resource.ValueMap/get 

Aanchal Sikka
Kamal_Kishor
Community Advisor
Community Advisor
January 9, 2024

Hi @jayv25585659 : I suppose you are using sling model to retrieve the field value as you have shared the sightly snippet.

Can you share how you are calling the sling model from your HTL please? It has to be something like this where myFieldNameHere would be the getter method which gives the field value of my-field-name-here.

<div data-sly-use.info="org.example.app.components.info.Info"> <p>${info.myFieldNameHere}</p> </div>

Or you can refer this link to see if you are doing it correctly.

https://experienceleague.adobe.com/docs/experience-manager-htl/content/java-use-api.html?lang=en#bundled-java-class
Sling Model documentation: https://sling.apache.org/documentation/bundles/models.html

 

kautuk_sahni
Community Manager
Community Manager
January 15, 2024

@jayv25585659 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni