Expand my Community achievements bar.

SOLVED

HashMaps in Sightly

Avatar

Level 1

I'm a beginner trying to learn AEM.
I've a Map<Custom class, Custom Class> from which I'm able to retrieve "Key" but not able to retrieve "Value" in Sightly.
If <div data-sly-list> is used it displays the "Key", but not displaying "Value".

Example :
<div>car class</div>
<div data-sly-use.test="${'Class Name' @ slingreq=request }">
           <div data-sly-list.var="${test.getVehFeatures}">Key: ${var.xyz} Value:${test.getVehFeatures[var].name}</div>

</div>

getVehFeatures is of type Map<Custom class, Custom class>
If <div data-sly-attribute > is used, it doesn't display anything. 

Please suggest an approach for this in Sightly.

Thanks,
Karthik

1 Accepted Solution

Avatar

Correct answer by
Level 10

When working with Collections  - why not use the Java part of a Sightly component? In this use case - the HashMap. Java has far better control reading a HashMap.  Read the HashMap in Java and pass the values back in JSON. 

See this community article to learn how to use Java and Sightly together: 

https://helpx.adobe.com/experience-manager/using/creating-sightly-component.html

See the Java class that extends WCMUse

View solution in original post

2 Replies

Avatar

Level 1

I am using sightly and java together.  I am having the same problem.  It seems as though sightly syntax does not allow the key of a hashmap to be anything other than a string.  In sightly when the key for my hashmap is a string I can make the original posters syntax work.  However when I make the key a custom class it not longer works.  (I did implement equals and hashcode in my custom class).  

I would like to know if its possible to make sightly syntax iterate a hashmap when the key of the hashmap is an custom class. 

Thanks

Avatar

Correct answer by
Level 10

When working with Collections  - why not use the Java part of a Sightly component? In this use case - the HashMap. Java has far better control reading a HashMap.  Read the HashMap in Java and pass the values back in JSON. 

See this community article to learn how to use Java and Sightly together: 

https://helpx.adobe.com/experience-manager/using/creating-sightly-component.html

See the Java class that extends WCMUse