Skip to main content

JSF Tables with POJOs in NetBeans 6.0

Posted by ddevore on February 12, 2008 at 7:24 PM PST

This is an addition to the JSF Visual Web JavaServer Faces in NetBeans Tutorial. For this information to be useful to you, you will have to have a JSF Visual Web JavaServer Faces project created. To create the project please refer to the earlier blog. Also, this tutorial is basically a continuation of the JSF Tables with databases in NetBeans 6.0 so you may want to also take a look at that it.

Useful Data

I know that a lot of people out there in the Java world will agree that the table and other components, such that are found in JSF, are not useful in larger installations if the data only comes directly from a database. Most larger installations will require a MVC architecture and will even hide the data behind services and data access layers. Because of this I will now show you how to bind the table used in the last blog to an array as might be returned by a service.

Data Model

First we must have a data object to work with so lets make one for the address. First I made a package named model under the existing jsf.tutorial package then a class Address with all the same fields as in the database, just to keep things consistent. If you are really observant you will see that I changed the structure of my address to be more international, I changed the zip to postalCode and added a countryCode. The following is the Address class that I will use.

package jsf.tutorial.model;

public class Address {
    private int id;
    private int type;
    private String line1;
    private String line2;
    private String city;
    private String state;
    private String postalCode;
    private int countryCode;
    private String note;

    public String getCity() {
        return city;

    public void setCity(String city) { = city;

    public int getCountryCode() {
        return countryCode;

    public void setCountry(int countryCode) {
        this.countryCode = countryCode;

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public String getLine1() {
        return line1;

    public void setLine1(String line1) {
        this.line1 = line1;

    public String getLine2() {
        return line2;

    public void setLine2(String line2) {
        this.line2 = line2;

    public String getNote() {
        return note;

    public void setNote(String note) {
        this.note = note;

    public String getPostalCode() {
        return postalCode;

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;

    public String getState() {
        return state;

    public void setState(String state) {
        this.state = state;

    public int getType() {
        return type;

    public void setType(int type) {
        this.type = type;

As a side note you must love the auto generation provided by IDEs.

Next we will put an array of the Addresses in the SessionBean1.

private Address[] addressArray;

public Address[] getAddressArray() {
    return addressArray;

public void setAddressArray(Address[] addressArray) {
    this.addressArray = addressArray;

Once we have done this we can then switch over to the Designer of Page1 and change the binding of the table to addressArray. If you right click on the table and select Bind to Data you will not see the addressArray though, which is a bug. For it to show up in the dropdown list in the Bind to Data dialog you must clean and build the project, do this now. Once it is cleaned and built you can then right click on the table and select Bind to Data and drop the list down and you will see addressArray (SessionBean1). Selecting this will give you a list of tables in alphabetical order which is not that hard to change, simply select the column and move it up or down. In this dialog you can also select which rows you do and do not want shown.

Other things you can do

You can select each column and change the header text to capitalize the first chars if you would wish, this being the most noticeable at first. But you can change just about any property from the property editor upon selecting the column or table. If you would like you can also go into the JSP selection to modify it directly but that isn't nearly as fun as doing it in the designer. The property I like the most is the pagination settings on the table itself. You can add pagination to the table easily and give the user control that would normally require a lot of work.

This is supposed to be able to work with Lists also but I have not seen it work yet. If anyone can confirm that it works with Lists and what build it works with please let me know.

That is it for now I hope you have enjoyed working with the tables with POJOs.


Hi, Dru. I am using a List instead of a array of Objects, and it works fine. However, I have a problem. The list is returned from a sophisticated select statement in a stored procedure, and the size of list might be large, 10,000 rows is normal. I just show 25 rows per page for my users, so I don't want 10,000 rows to be loaded each time my users just request a next page. So my question is: if it is possible for me to tell the table how many rows in total there are, and I will let the stored procedure return only one page each time? Thank you very much.

Dear Dru, very good example!! :-) May I ask a step more? My web service reply with a variable size object table (i.e. Object[][]), and in this case how can I do?? Of course I know the number of rows and columns, but it is not known in advance, it depend on the db query. Is it preferable to build all the columns and hide the not interesting columns before visualization?? I'm really new in this so please could you explain an example? Many thanks in advance!!! Cris

Dru, I have it working using a RichFaces table so I am going that way. Disregard the question/issue. Dan

Dru,'s the example. MyData is a POJO class in the web project. In protected MyData[] myData; public MyData[] getMyData() {return myData; } public void setMyData(MyData[] myData) { this.myData = myData;} This works in the table. Now, Record comes from a library which I add in the properties. Record is a POJO class...nothing special. It's defined in as: public Record[] myRecordArray; public Record[] getRecordArray() {return myRecordArray;} public void setRecordArray(Record[] myRecordArray) { this.myRecordArray = myRecordArray; } Record doesn't work and can't be seen when trying to use the "Bind to Data" or "Table Layout" for the table. I was able to get it working using a wrapper but seems silly to me. Need to see src? Thx, Dan

ynotlim, Sorry for the delay I forgot about your question. To get data in the table you must use the setAddressArray(Address[] addressArray) method in SessionBean1. The binding will take care of the rest for you. Dan, You should be able to use a POJO directly though you will have to set it in an object that is accessible by the jsp the table is in such as the session bean as above. The project I am working on now has the data coming from a generated class from a web service call. So as long as the data makes it to the SessionBean1, as in this example, the data would be bindable to the table. If you are still having problems with it if you could provide an example I would be happy to see if I can see what is wrong. Any more questions please let me know.

Dru, I can get this to work if the POJO is within the web app. If it's outside of the web project and the library is correctly defined in the props, it doesn't allow it to be used. I would like to use jars from other NB project if possible. I don't want to use a wrapper but maybe that's the only choice?? Any thoughts on how to avoid using wrappers? Dan

hi. did u skip any steps in here in order to make it faster or for any reasons. i dont get this working. as i dont have my array populated from the databases. did u skip a method that does this.? Thanks Su

Good find. I didn't even try it with inner classes thanks for the input.

thanks for the helpful guide! a small discovery I made, which I hope will save others time: the 'Bind to Data' option for a table will not find an array of any inner class- it must be an outer class.

Hello thickbrain. I'm using NB 6.1 as well and no problem appeared. I think (if I understand your description correctly) that the problem is that you "added the addressArray object and methods in my main class". As Dru writes you need to have them in file.

hi Dru, thanks for the post. How do I set all the variables in to show up on my table? My table keeps coming up with "no items found". thanks

Hi Dru. I cannot get it to show in the "Binding Source" list. I'm using Netbean 6.1. I created an Address class in its own file and added the addressArray object and methods in my main class. The main class finds the Address class. I also did the clean and build. Is 6.1 might be the problem?

Cris, First a question about the problem. You said that there is a number of rows/columns that are returned but you don't know the size in advance. If you don't know the number of columns in the table it would be difficult to make the bindings using faces. If you have a couple of possibilities then you could develop a couple of different ones and show/hide them as required. Another way of doing this could be to use JSTL to conditionally show different columns in the array. If you could provide a little more detail I might be able to help more.