Forums Register Login

How to prepopulate combo boxes on page startup

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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"));
+Pie Number of slices to send: Send
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;

}
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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']}" />
+Pie Number of slices to send: Send
 

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".
+Pie Number of slices to send: Send
Thanks a lot for the replies.
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5587 times.
Similar Threads
selectOneMenu caching issue
how to render a component based on condition
populating combo in JSF page
Best way to create a combo-box (editable selectBox)?
selectOneMenu label value
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:57:49.