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 JSF drop down. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF drop down." Watch "JSF drop down." New topic
Author

JSF drop down.

Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
Hi,

This is a very basic question !

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)
Bagwan Mehrat
Ranch Hand

Joined: Jan 26, 2002
Posts: 119
Originally posted by Raj Ravi:




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
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

StundentBean has a method

public List getStudents() {

students.add(new SelectItem("101", "name1"));
students.add(new SelectItem("102", "name2"));
}

id attribute of the <selectOneMenu /> specifies the value to be selected.
if the value is 102, the name2 of dropdown will be selected by default.

I hope this will be helpful for people who are new to JSF. Still wondering why i cldn't find any examples on the same !

Thanks,
Rajeev
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
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
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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
i dont think JSF is that easy for a newbie. i got into another problem with <selectOneMenu>

if i hardcode the options like



but



is throwing java.util.NoSuchElementException

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
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 !!

thanks,
Rajeev
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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
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 !

Thanks alot for your help and support.

Rajeev.
Anoop V. Kumar
Greenhorn

Joined: Jan 21, 2006
Posts: 18
Hey this is very very useful...

Just going one step further - how can I retrieve the item that the user selected from the drop down??

Thanks,
Anoop
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Good question.
"How can I retrieve the item that the user selected from the drop down??"


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Prasanna Chennupati
Ranch Hand

Joined: Feb 09, 2006
Posts: 38
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.

For example ,look at the following code.

<h:selectOneMenu id="dropdownId"
value="#{valueObject.dropdownName}" styleClass="selectOneMenu" >
<f:selectItem itemValue="" itemLabel="#{msg.selectOne}" />
<f:selectItems
value="#{pc_AppHHMedicaidElderlyPersonDisabilaties.selectInstituteType.instituteType}" />
</h:selectOneMenu>


Thanks,<br />Prasanna<br />SCJP1.4,SCWCD1.4,SCBCD1.3,SCDJWS,SCEA-1<br />IBM Certified Solution Developer -- WebSphere Portal V5.1(got 100%)
Prasanna Chennupati
Ranch Hand

Joined: Feb 09, 2006
Posts: 38
I am sorry for my incorrect reply.
Incompletely I added my reply in the middle.Please ingnore that one.

Am extremely sorry for my incorrect reply
DeAlton Jones
Greenhorn

Joined: Apr 04, 2006
Posts: 22
While we are on the topic

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
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.

Thanks,
[ April 05, 2006: Message edited by: Rajeev Ravindran ]
Prasanna Chennupati
Ranch Hand

Joined: Feb 09, 2006
Posts: 38
Hi Jin,

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.

Hope this will help you.

Thanks,
Prasanna
Knolf Moervenson
Greenhorn

Joined: Jul 25, 2008
Posts: 12
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
Knolf Moervenson
Greenhorn

Joined: Jul 25, 2008
Posts: 12
Got it, silly question..
:p
Brian Yi
Greenhorn

Joined: Aug 18, 2009
Posts: 1
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
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
What is the package to import from for SelectItem in the backing bean?

Thanks,

Sonia
ali hammad
Greenhorn

Joined: Dec 03, 2009
Posts: 5
@Gregg Bolinger Thank you very much. Your code solved my problem. Thanks alot
Namita Nair
Greenhorn

Joined: Mar 25, 2010
Posts: 3
I am facing an wired issue that one of the developers have also faced in the message above.

When i change the drop down to fetch vales from the backing bean on change of the drop down value i get the below error:

java.util.NoSuchElementException

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.
henry xu
Greenhorn

Joined: Apr 14, 2010
Posts: 2
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
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.

Thanks and Regards,

Namita
Anand Shankar
Greenhorn

Joined: Aug 03, 2010
Posts: 2
Hi

You can do it as given code




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



Best Wishes:

Anand Shankar
ACIIL, IGNOU

srikanth puchakayala
Greenhorn

Joined: Feb 19, 2009
Posts: 6
This url might be helpful for beginners
http://balusc.blogspot.com/2007/10/populate-child-menus.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF drop down.