Show HashMap in Datatable. UIColumn with embedded Datatable. Help Needed
Hi,
I am trying to display a HashMap in a datatable where the values for each key is an ArrayList e.g.
ArrayList al =new ArrayList();
al.add("AA");
al.add("BB");
al.add("CC");
HashMap hm =new HashMap();
hm.put("ONE", al);
ArrayList al2 =new ArrayList();
al2.add("AA2");
al2.add("BB2");
al2.add("CC2");
hm.put("TWO", al2);
Now I have a Datatable with two columns. First column shows the key and the second column I again have
a datatable to show the ArrayList. So I will have one row with a key and the values again shown in a
datatable. So far I no luck and need some help on how to achieve this. Here is the index2.jsp code I have:
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=windows-1252"/>
<title>index2</title>
</head>
<body><h:form binding="#{backing_index2.form1}" id="form1">
<h:dataTable value="#{backing_index2.entrySet}" var="var1"
binding="#{backing_index2.dataTable1}" id="dataTable1" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Keys"/>
</f:facet>
<h:outputText value="#{var1.key}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Values"/>
</f:facet>
<h:dataTable value="#{backing_index2.entrySet}" var="var2"
binding="#{backing_index2.dataTable2}" id="dataTable2">
<h:column id="col2">
<h:outputText id="out3" value="#{var2.value}"/>
</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
</h:form></body>
</html>
</f:view>
And here is the backing bean Index2.java I have:
package project1.backing;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlForm;
publicclass Index2{
private HtmlForm form1;
private HtmlDataTable dataTable1;
private HtmlDataTable dataTable2;
private List entrySet;
private HashMap hm;
public Index2(){
ArrayList al =new ArrayList();
al.add("AA");
al.add("BB");
al.add("CC");
hm =new HashMap();
hm.put("ONE", al);
ArrayList al2 =new ArrayList();
al2.add("AA2");
al2.add("BB2");
al2.add("CC2");
hm.put("TWO", al2);
entrySet =new java.util.ArrayList(hm.entrySet());
//here it only prints two rows as i have two keys but on the output it
//does not come up fine
for (int i = 0; i < entrySet.size(); i++){
System.out.println("==== " + entrySet.get(i));
}
}
publicvoid setForm1(HtmlForm form1){
this.form1 = form1;
}
public HtmlForm getForm1(){
return form1;
}
publicvoid setDataTable1(HtmlDataTable dataTable1){
this.dataTable1 = dataTable1;
}
public HtmlDataTable getDataTable1(){
return dataTable1;
}
publicvoid setDataTable2(HtmlDataTable dataTable2){
this.dataTable2 = dataTable2;
}
public HtmlDataTable getDataTable2(){
return dataTable2;
}
publicvoid setEntrySet(List entrySet){
this.entrySet = entrySet;
}
public List getEntrySet(){
return entrySet;
}
publicvoid setHm(HashMap hm){
this.hm = hm;
}
public HashMap getHm(){
return hm;
}
}
Now when I run this application the keys are shown in the respective column but values are coming up as
[AA2, BB2, CC2] in one row rather in a separate Row as I am using a Datatable inside UIColumn to show the values.
Here is what I see in the output:
keysValues
TWO [AA2, BB2, CC2]
[AA, BB, CC]
ONE [AA2, BB2, CC2]
[AA, BB, CC]
As above output is not correct as it shows both list values in front of each key rather than what those
keys are tied to. I wanted the following output:
keysValues
ONE AA
BB
CC
TWO AA2
BB2
CC2
So I can do sorting pagination on the values datatable. But currently the output is not shown correctly. Any help
is really appreciated. My hashmap will be populated dynamically so wants to show the key and values in separate column
but also need to show the values in an embedded datatable as the list can be huge.

