The Source for Java Technology Collaboration
User: Password:



Dru Devore

Dru Devore's Blog

JSF Tables with POJOs in NetBeans 6.0

Posted by ddevore on February 12, 2008 at 07:24 PM | Comments (5)

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) {
        this.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) {
        this.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.

Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • 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.

    Posted by: yonestar on February 13, 2008 at 02:01 PM

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

    Posted by: ddevore on February 13, 2008 at 02:34 PM

  • 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?

    Posted by: thickbrain on March 30, 2008 at 08:34 AM

  • 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 SeesionBean1.java file.

    Posted by: sheeryjay on May 01, 2008 at 10:17 PM

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

    Posted by: ynotlim on July 29, 2008 at 03:41 PM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds