How to Bind an Entity Bean to a DropDownList

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view

How to Bind an Entity Bean to a DropDownList

Environment: NB 6.1, Vista OS, MySql 5.1, GlassFish.

Over the past day I have sought clear direction as to how to bind a JPA entity bean to a dropdownlist. I examined the following Nabble links for assistance:

Here is what I have done thus far:

<entity bean>
I used the IDE to auto-gen the POJO entity which is a simple lookup table in the Db:
     id            unsigned int,
     domain     varchar(50)
     value       varchar(255)
</entity bean>

<in an entity manager>
public List getCountryLookups() {
        Query query = _EM.createNamedQuery("Lookup.findByDomain");
        query.setParameter("domain", "Country");
        return query.getResultList();
</in an entity manager>

<entended an ObjectListDataProvider>
public class CountriesDataProvider extends ObjectListDataProvider {

    private static ArrayList _ArrayList;
    public CountriesDataProvider() {
        if (_ArrayList == null) {
            FacesContext fc = FacesContext.getCurrentInstance();
            _ArrayList = new ArrayList();
            _ArrayList.add((List<Lookup>) fc.getExternalContext().getApplicationMap().get("countries"));
</entended an ObjectListDataProvider>

<getter and setter in page fragment>
private CountriesDataProvider _Countries = new CountriesDataProvider();

    public CountriesDataProvider getCountries() {
        return _Countries;
</getter and setter in page fragment>

Now this is where things get hazy... I have binded the dropdownlist to the getcountries property - but the rendering didn't work.

So my questions are:

1. Bind to the selectItems node?
2. Is there a better method for binding an entity bean to the ddl?
Reply | Threaded
Open this post in threaded view

Re: How to Bind an Entity Bean to a DropDownList

After poking around (NB team there is very poor documentation for this) I crafted a solution...

First off, let me say that I am not using the non-stylable WoodCrap DrowDownList component. Rather, I am implementing a Standard DropDownList.

I pulled my lookup table values in ServletListener.contextInitialized() and stored those values as an arraylist of SelectItems in the ServletContext to save on Session bloat:

        ServletContext sc = event.getServletContext();

        // Global Db data cache in VM
        Coordinate coordinate = new Coordinate();
        // Countries
        List list = (List<Lookup>) coordinate.getCountryLookups(); // see entity manager above
        ArrayList CountryAL = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            Lookup lookup = (Lookup) list.get(i);
            CountryAL.add(new SelectItem(lookup.getLookupid().toString(), lookup.getValue()));
        sc.setAttribute("countries", CountryAL);
        sc.setAttribute("default country", "237");

Secondly, I created get properties in my page fragment:

public ArrayList getCountries() {
        FacesContext fc = FacesContext.getCurrentInstance();
        return (ArrayList) fc.getExternalContext().getApplicationMap().get("countries");
    public String getDefaultCountry() {
        FacesContext fc = FacesContext.getCurrentInstance();
        return (String) fc.getExternalContext().getApplicationMap().get("default country");

Lastly, I did the binding using the page fragment design object tree view...
1. select the DDL > right click > Property Bindings > select DefaultCountry as the binding target.
2. expand the DDL. You now see the f:selectItems name (mine is called ddlCountryItems).
3. select the Items name > right click > Property Bindings > select countries as the binding target.

If NB acts buggy save, close, reopen, clean and build.

Compile and life should be all good. I hope this really does help out someone.