Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to prepopulate combo boxes on page startup

 
Megan Troy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the replies.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic