tags:

views:

63

answers:

1

Hi,

I want to place a table on my page. I have two tables in my database for example users and locations. Every location has more than one user. I want to list these locations and show the users who live in these locations.


Los Angeles

  1. John Locke
  2. Dr. Jack
  3. Mr. Eco

Like the below image, could someone do this in JSF?

Thanks.

A: 

You can use RichFaces to do such thing:

<h:form>
<rich:dataList var="city" value="#{myBean.allCity}">
    <h:outputText value="#{city.name}" ></h:outputText>
    <rich:dataList var="user" value="#{city.users}">
         <h:outputText value="#{user.name}" ></h:outputText>
    </rich:dataList>
</rich:dataList>
</h:form>

Where allCity - list of the City, and every City has list of the user inside.

See http://livedemo.exadel.com/richfaces-demo/richfaces/dataLists.jsf?c=dataList&amp;tab=usage for example.

Backing bean:

myBean:

public class MyBean(){
   private ArrayList<City> allCity= new ArrayList<City>();

   @PostConstruct
   public void init(){
     //fill Array list
   }

    public ArrayList<City> getAllCity() {
     return allCity;
    }      

}

City:

public class City{
   private ArrayList<User> users= new ArrayList<User>();

   public City( ArrayList<User> users){
     this.users = users;
     // you can get data from database in myBean, and pass it hear with cinstructor;
   }

   public ArrayList<User> getUsers() {
     return allCity;
   }     
}

User

public class User{
  private String name;

  //constructor and others fields;

  public String getName(){
     return name;
  }
}

Only MyBean you register as backing-bean. I show you only base structure of class, how you fill it with data is you choice.

masterzim
could you please write the code in the backing bean/beans?
Watch changes atop.
masterzim
Thanks a lot. I did it as you said and everything is ok.
I see you new hear, if you accept answer click on button \/. You should do this in all you questions. :)
masterzim