Display hashmap/map content in a JSF Page
Map<K,V> is an object that maps keys to values, and Hashmap is the class that implements the interface Map. In order to display the contents of a “Map” or a “Hashmap” in a java server faces page, we have to use a list of either an entitySet or keySet of the “Map” and iterate through that list.
The following codes display the contents of the hashmap:
JSF Managed Bean:
beanName: “testMap”
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class testMap{
private Map<String, List<String>> stringMap;
private List entrySet;
public List getEntrySet() { //getter of entrySet list
stringMap = new HashMap<String, List<String>>();
List<String> str = new ArrayList<String>();
str.add(“One”);
str.add(“Two”);
str.add(“Three”);
stringMap.put(“ones”, str);
List<String> str1 = new ArrayList<String>();
str1.add(“Eleven”);
str1.add(“Twelve”);
str1.add(“Thirteen”);
stringMap.put(“tens”, str1);
List<String> str2 = new ArrayList<String>();
str2.add(“Twenty One”);
str2.add(“Twenty Two”);
stringMap.put(“twenties”, str2);
entrySet = new ArrayList(stringMap.entrySet());Iterator it=entrySet.iterator();
while(it.hasNext()){
System.out.println(“… it.next: “+it.next());
}
return entrySet;
}public void setEntrySet(List entrySet) { //setter of entrySet list
this.entrySet = entrySet;
}
}
... it.next: twenties=[Twenty One, Twenty Two] ... it.next: tens=[Eleven, Twelve, Thirteen] ... it.next: ones=[One, Two, Three] ... it.next: twenties=[Twenty One, Twenty Two] ... it.next: tens=[Eleven, Twelve, Thirteen] ... it.next: ones=[One, Two, Three] ... it.next: twenties=[Twenty One, Twenty Two] ... it.next: tens=[Eleven, Twelve, Thirteen] ... it.next: ones=[One, Two, Three]
The following two JSF Pages will display the values of the Map or HashMap:
<%@taglib prefix=”f” uri=”http://java.sun.com/jsf/core”%>
<%@taglib prefix=”h” uri=”http://java.sun.com/jsf/html”%>
<f:view>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<body>
<h:dataTable border=”1″ value=”#{testMap.entrySet}” var=”keys” >
<h:column> <f:facet name=”header” > <h:outputText value=”Map Display Using DataTable”/> </f:facet>
<h:dataTable border=”1″ value=”#{keys}” var=”type” >
<h:column> <f:facet name=”header” > <h:outputText value=”Key”/> </f:facet> <h:outputText value=”#{type.key}”/> </h:column>
<h:column> <f:facet name=”header” > <h:outputText value=”Value”/> </f:facet>
<h:dataTable value=”#{type.value}” var=”num” >
<h:column> <h:outputText value=”#{num}”/> </h:column>
</h:dataTable>
</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
</body>
</html>
</f:view>
Output:
OR,
<%@taglib prefix=”f” uri=”http://java.sun.com/jsf/core”%>
<%@taglib prefix=”h” uri=”http://java.sun.com/jsf/html”%>
<f:view>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<body>
<h:dataTable border=”1″ value=”#{testMap.entrySet}” var=”entry” >
<h:column> <f:facet name=”header”> <h:outputText value=”Key”/> </f:facet>
<h:dataTable value=”#{entry}” var=”type” >
<h:column> <h:outputText value=”#{type.key}”/> </h:column>
</h:dataTable>
</h:column>
<h:column> <f:facet name=”header”> <h:outputText value=”Value”/> </f:facet>
<h:dataTable value=”#{entry}” var=”values” >
<h:column> <h:dataTable value=”#{values.value}” var=”num” >
<h:column> <h:outputText value=”#{num}”/> </h:column>
</h:dataTable>
</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
</body>
</html>
</f:view>
Output:
Unfortunately, I haven’t been able to sort and/or display the hashmap/map as entered. The outputs are either jumbled or like a stack, where the last entry is printed at the first. If anyone is able to find a solution for sorting, please post a link in the comments or put the code in the comments. Thank You.
References: