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

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

http://www.nabble.com/Visual-Web-Designer%3A-Bind-an-EJB3-entity-bean-to-a-dropdown-list-td14888317.html#a14920128
http://www.nabble.com/Dropdown-list-of-user-tp11783679p11801513.html

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"));
            setList(_ArrayList);
        }
    }
}
</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

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

<code>
        ServletContext sc = event.getServletContext();

        // Global Db data cache in VM
        Coordinate coordinate = new Coordinate();
        coordinate.Initialize();
       
        // 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");
</code>

Secondly, I created get properties in my page fragment:

<code>
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");
    }
</code>

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.