Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

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

Avatar

Level 9

So this works ok in HTL/sightly

 

<h3>${properties['my-field-name-here'] @CONTEXT='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()
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

@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

Avatar

Community Advisor

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#bun...
Sling Model documentation: https://sling.apache.org/documentation/bundles/models.html

 

Avatar

Administrator

@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