×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Show HashMap in JSF Datatable. UIColumn with embedded Datatable

Show HashMap in JSF Datatable. UIColumn with embedded Datatable

Show HashMap in JSF Datatable. UIColumn with embedded Datatable

(OP)
Hi,

I am trying to display a HashMap in a datatable where the values for each key is an ArrayList e.g.

CODE

        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:

CODE


<!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:

CODE


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;


public class 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));
        }
    }

    public void setForm1(HtmlForm form1) {
        this.form1 = form1;
    }

    public HtmlForm getForm1() {
        return form1;
    }

    public void setDataTable1(HtmlDataTable dataTable1) {
        this.dataTable1 = dataTable1;
    }

    public HtmlDataTable getDataTable1() {
        return dataTable1;
    }

    public void setDataTable2(HtmlDataTable dataTable2) {
        this.dataTable2 = dataTable2;
    }

    public HtmlDataTable getDataTable2() {
        return dataTable2;
    }

    public void setEntrySet(List entrySet) {
        this.entrySet = entrySet;
    }

    public List getEntrySet() {
        return entrySet;
    }

    public void 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:

CODE

keys        Values
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:

CODE

keys        Values
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.

Thanks
Replies continue below

Recommended for you

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close