All blog posts, code samples and downloads licensed under Apache License 2.0.
Close

Iterating through a HashMap in a repeat control

Oliver Busse on 11/07/2013 10:15:34 PM CET, filed under XSP Java 

Today I got stuck with a repeat control that should loop over a HashMap. My first approach lead me into the trap that I only got the key values as String within the repeat body (e.g. as a value for a label). But it is really simple if you do not forget to adapt the repeat data source to use the entrySet() instead of using the Map itself.

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
	<xp:repeat id="repeat1" rows="30" var="entry">
		<xp:this.value><![CDATA[#{javascript:var map = new java.util.HashMap();
map.put("foo", "bar");
map.put("blah", "blubb");
return map.entrySet();}]]></xp:this.value>
		<xp:label value="#{javascript:entry.getKey()}" id="label2"></xp:label> -
		<xp:label value="#{javascript:entry.getValue()}" id="label1"></xp:label>
		<xp:br></xp:br>
	</xp:repeat>
</xp:view>

If you use only the Map itself as source then the collection variable will only contain the key value as a simple string, so you won't be able to access the value of the map entry.

Lessons learned...

Thanks to @TheGreenRobot for pointing me to the right direction L├Ąchelnd


Tagged with java xpages repeat control hashmap entryset