File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes How to prepopulate combo boxes on page startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to prepopulate combo boxes on page startup" Watch "How to prepopulate combo boxes on page startup" New topic
Author

How to prepopulate combo boxes on page startup

Megan Troy
Greenhorn

Joined: Jul 27, 2005
Posts: 10
Hi,
I am new to JSF and struggling to understand how a combo box can be prepopulated on page startup.

My code is as follows:
<h:selectOneMenu id="selectCar" value="#{Cars.currentCar}">
<f:selectItems value="#{Cars.carList}" />
</h:selectOneMenu>

The carListproperty should be set with data from the database.

Can the Cars managed bean make a call to the server side to populate itself or is there somekind of a controller to do the job before the page is displayed.

Thanks in advance

Abhi Raj
Greenhorn

Joined: Sep 08, 2004
Posts: 7
JSF uses the method binding to get a list to populate the combo box. So just have the getCarList() method return the list you want e.g. by creating the list itself, or calling some other object which does so.
Prem Khan
Ranch Hand

Joined: May 30, 2005
Posts: 189
And you must make sure you fill the list up with the right class

this.myList.add(new SelectItem("The Object that you want the value in the bean to equal in this case its a string ","MyLable"));
Prem Khan
Ranch Hand

Joined: May 30, 2005
Posts: 189
and remember to fill that list up before the page forwarded to the page you
wanna load.

maybee do this

public String prepare()
{
1. //fill the private method with the class i listed above

2. String returnValue = "whatever the String you declared in your
facesconfig.xml to navigate to the desired page"

return returnValue;

}
Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
OK, related question. How do you get the combo box to select the current value from the bean on page population? I can't seem to get the list to coordinate with the value chosen. Thanks
Mauro Castaldo
Greenhorn

Joined: Jul 18, 2005
Posts: 22
Originally posted by Paul Smiley:
OK, related question. How do you get the combo box to select the current value from the bean on page population? I can't seem to get the list to coordinate with the value chosen. Thanks


Can you explain better what do you intend?
Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
Well, I have a HtmlSelectOneMenu component that has a value of 1 member of the backing bean and the SelectItem options are populated with another member which is a list of potential values. When the page renders, the first element of the list always displays in the list, not the current value as held in the backing bean element 1. It seems like some type of translation to the Html Option selected="true" needs to be rendered, but I don't know how to do it. It seems like the data element is not bound to the component.
Thanks,
Paul
Megan Troy
Greenhorn

Joined: Jul 27, 2005
Posts: 10
Originally posted by Shawn DeSarkar:

public String prepare()
{
1. //fill the private method with the class i listed above

2. String returnValue = "whatever the String you declared in your
facesconfig.xml to navigate to the desired page"

return returnValue;

}




Thanks for the replies.
Could someone explain where this prepare() method is declared?

Is it in the backing bean for the JSF page or in the managed bean which is Cars in my example?

Either way who calls this method?

Still
Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
Megan,
You have to put it in a form and put the action on a button or link

<h:commandButton action="#{beanName.prepare}" value="#{messages['button_save']}" />
Mauro Castaldo
Greenhorn

Joined: Jul 18, 2005
Posts: 22
Originally posted by Paul Smiley:
Well, I have a HtmlSelectOneMenu component that has a value of 1 member of the backing bean and the SelectItem options are populated with another member which is a list of potential values. When the page renders, the first element of the list always displays in the list, not the current value as held in the backing bean element 1. It seems like some type of translation to the Html Option selected="true" needs to be rendered, but I don't know how to do it. It seems like the data element is not bound to the component.
Thanks,
Paul


This code run correctly on JSF RI 1.1_01:


the HtmlSelectOneMenu correctly select "Item2".
Megan Troy
Greenhorn

Joined: Jul 27, 2005
Posts: 10
Thanks a lot for the replies.
 
Don't get me started about those stupid light bulbs.
 
subject: How to prepopulate combo boxes on page startup