0

I am using netbeans 6.9.1 Glassfish 3. I have a JSF file, and there is a selectOneMenu listbox on it. this list box will be populated with values from the DB.

For now, when i click on a button on the form, the values get added to the listbox. What i want to do is, to populate the list box when the page loads it self.

Any idea how i should do this. I tried adding code to the Java constructor so it would call it, but it didn't work. (I get a warning asking to Make the class final - if i do this, i can't access any methods from my JSF)

The warning i received - overridable method calls in constructors

The code is too big i can't put it here, i need to know a solution to overcome this

Illep
  • 16,375
  • 46
  • 171
  • 302

2 Answers2

1

Any idea how i should do this. I tried adding code to the Java constructor so it would call it, but it didn't work. (I get a warning asking to Make the class final - if i do this, i can't access any methods from my JSF)

The warning i received - overridable method calls in constructors

That's just a Netbeans warning, not a Java compilation error. Your code should compile and run just fine. Netbeans is just trying to be smart and hint you about a potential design problem. Your constructor is calling an abstract method of the very same class. Whether that's bad depends actually on the remnant of the design which we know nothing about, but you should realize that it can potentially lead to bugs and errors in the code. To me, it makes at least no sense why you would call an abstract method to prepopulate a dropdown, so probably this is not related at all.

Ignore and run it. Or rethink your design approach.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0
  1. Create some ManagedBean class and give it List of SelectItem (make it property)
  2. Refer this property from your jsf (xhtml) page

And items should now be loaded now.

@ManagedBean
public class MyBean {
  private List<SelectItem> items; // populate from DB
  public List<SelectItem> getItems() { return items; }
  // no need to setter
}
<h:selectOneMenu value="#{myBean.whatever}">
    <f:selectITems value="#{myBean.items}" />
</h:selectOneMenu>

Edit: About the populating values ...

Usually you have database service and instantiate it (or inject it) to bean and fill items, something like items = service.loadItems().

Maybe you might want to iterate over them, because you need to create SelectItem value with String constructor :)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Xorty
  • 18,367
  • 27
  • 104
  • 155
  • Yes, i have this. But don't we require to add the populate from DB part in the Constructor of MyBean Class ? – Illep Jul 31 '11 at 11:19
  • of course you do. Make no arg constructor and add the logic there. – Xorty Jul 31 '11 at 11:22
  • I got this error when i added the logic to the constructor. SEVERE: doSelect IOException java.net.BindException: Address already in use: bind: 7676=com.sun.enterprise.v3.services.impl.ServiceInitializerHandler@18efc0c Any clue ? – Illep Jul 31 '11 at 11:28
  • Try to put there some dummy values and create dummy SelectItems (like "joe", "john", "scott") and see if that works. If it does, problem is in populating items from DB. If it doesn't, problem is in the bean class or JSF page. – Xorty Jul 31 '11 at 11:37