I have an object Array , for example Student[] Student has 2 properties, id and name i want id to be the value and name to be the display text of a drop down box.
Could someone tell me how to do this in JSF. I didnt not find any examples for populating drop down with dynamic values. It wll be really helpful if someone has time to explain or provide a link that explains the attributes of selectOneMenu and selectItem. My code is incorrect by im just pasting it here.
Thanks, Raj
SCJP, SCWCD, SCBCD, Oracle Certified Professional (SQL n PL/SQL)
I believe you need something like <f:selectItems value=#{studentBean.studentList} /> where studentList would return a collection of javax.faces.model.SelectItem objects.
Rajeev Ravindran
Ranch Hand
Joined: Aug 27, 2002
Posts: 455
posted
0
hi bagwan, i appretiate your reply.. i atlast found it out, its strange that i could not find any good examples. i know its a very basic requirement
StudentBean is a ManagedBean we define in faces-config.xml, students is an arraylist variable declared in StudentBean
Not that this is way different but I prefer to use component bindings. It keeps the JSP cleaner, however, adds a bit to the Java code. Here is an example.
Note that here I am using Java1.5 features and Spring to get my data from the database. Like I said, it's a bit more java code to do it but it keeps the JSP's a bit cleaner, IMHO. It's always good to have more than 1 option anyway.
Rajeev Ravindran
Ranch Hand
Joined: Aug 27, 2002
Posts: 455
posted
0
Gregg, thanks for the code.. Could you tell me where i can find more information on component biding (other than the sun site)
Originally posted by Rajeev Ravindran: Gregg, thanks for the code.. Could you tell me where i can find more information on component biding (other than the sun site)
Thanks again, Rajeev
When I was learning JSF I lived and breathed by JSF in Action by Kito D. Mann. If you can, get that book. EBook is cheaper. It's from Manning.
Rajeev Ravindran
Ranch Hand
Joined: Aug 27, 2002
Posts: 455
posted
0
i dont think JSF is that easy for a newbie. i got into another problem with <selectOneMenu>
Could anyone help me with this ? if i look at view source of both jsp pages they look same.
Thanks, Rajeev
Rajeev Ravindran
Ranch Hand
Joined: Aug 27, 2002
Posts: 455
posted
0
sorry i forgot to specify when im getting error. The page is getting displayed properly with the data being populated on the drop down box, but when i click on the submit button this error is thrown. The action hasnt reached the doSubmitAction() of the button, i hope im not confusing you all !!
It probably has something to do with the properties of your backing bean. It would help if you could show us your backing bean code.
Rajeev Ravindran
Ranch Hand
Joined: Aug 27, 2002
Posts: 455
posted
0
Gregg, i found out the issue. The problem was due to the scope i set for managed bean in faces-config.xml. When I change the scope from request to session, it worked fine !
You need to bind your dropdown to a VO(Value Object)on the front end. Once the user selects a value from the drop down it will automatically set the value to that VO and You can call getter on that VO to get the value of dropdown.
Is there an easy way to populate the dropdown from a database?
thanks in advance
Rajeev Ravindran
Ranch Hand
Joined: Aug 27, 2002
Posts: 455
posted
0
Jones, didn't you notice the code by Gregg ? I beleive it is a good way to populate dropdown from database. Other options is to make the DB call from your managed bean and return a List that holds the database values.
Managed bean contains getters and setters for the fields in the JSF page. Inorder to get the value from the dropdown,you can just call getter method of that dropdown.
As we bind the managed bean to the JSF page,whenever the user selects a value from dropdown,it will set the value for the dropdown automatically in managed bean.
All you need to do is just call setter method of the dropdown. I am also a newbie to JSF.
Hi Gregg! I like the code you posted concerning selectOneMenu binding in jsf keeping clean the jsp-code. So I tried to use it and adjust the code to my project, but now I got a PropertyNotWritableException. Think a setter is needed in the backingBean, but I'm new in jsf and for me it's not obvious how this setter should look like.. would be great if you could help me with a setter code handling the selected id.
Knolf Moervenson wrote:Hi Gregg!
I like the code you posted concerning selectOneMenu binding in jsf keeping clean the jsp-code. So I tried to use it and adjust the code to my project, but now I got a PropertyNotWritableException. Think a setter is needed in the backingBean, but I'm new in jsf and for me it's not obvious how this setter should look like.. would be great if you could help me with a setter code handling the selected id.
Thanks a lot and greets,
Henning
I too have this question. I don't quite understand why the bind is affexting the selection. Thanks
Brian
I dont want to change my backing bean to session scope i want to keep it as request scope bean.
In my backing bean on change of the value i am populating the drop down values again.
Can anyone suggest on this.Is there any other alternative.
Gregg Bolinger wrote:Not that this is way different but I prefer to use component bindings. It keeps the JSP cleaner, however, adds a bit to the Java code. Here is an example.
Note that here I am using Java1.5 features and Spring to get my data from the database. Like I said, it's a bit more java code to do it but it keeps the JSP's a bit cleaner, IMHO. It's always good to have more than 1 option anyway.
Namita Nair
Greenhorn
Joined: Mar 25, 2010
Posts: 3
posted
0
Hi,
I have a requirement for changing the background of the value in h:SelectOneMenu when the mouse is over the value.I mean the value which the mouse points to should be highLighted i.e if have 10 values in the drop down when my mouse is on the second value in dropdown the value should be highlighted in yellow whereas the others should not be highlighted.
I tried using onmouseover attribute but it jut mess up my logic when mouse is over the dropdown all values are highLighted.
write the given method in mybean class.
This method will be called automatically when value change in drop down box and that value will be returned in given method. Now set it in any Text Field or as wish