Cq5 – how to get key / value from Java use class HashMap
                                        
                    •
                    Java                                    
                I have a basic Java usage class object that extends wcmuse and a simple HashMap method - in obvious code - I have something similar
${} item
${} item. key
${} item. value
Not working – how to return key / value pairs in obvious code
Solution
There is an example at visibility intro Part 3. AEM docs visibility page records the use of ${item} and ${itemlist} as variables This page also provides the following examples for accessing dynamic values:
<dl data-sly-list.child="${myObj}">
<dt>key: ${child}</dt>
<dd>value: ${myObj[child]}</dd>
</dl>
This is a simple HashMap example
HTML with visibility:
<div data-sly-use.myClass="com.test.WcmUseSample" data-sly-unwrap>
    <ul data-sly-list.keyName="${myClass.getMyHashMap}">
        <li>KEY: ${keyName},VALUE: ${myClass.getMyHashMap[keyName]}</li>
    </ul>
</div>
Java:
package com.test;
import java.util.HashMap;
import java.util.Map;
import com.adobe.cq.sightly.WCMUse;
public class WcmUseSample extends WCMUse {
private Map<String,String> myHashMap;
    public void activate() throws Exception {
        myHashMap = new HashMap<String,String>();
        for (int i = 0; i < 10; ++i) { 
            myHashMap.put(""+i,"Hello "+i);
        }
    }
    public Map<String,String> getMyHashMap() {
        return myHashMap;
    }
}
                
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        