• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSF drop down.

 
Rajeev Ravindran
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bagwan Mehrat
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question.
"How can I retrieve the item that the user selected from the drop down??"
 
Prasanna Chennupati
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Prasanna Chennupati
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, silly question..
:p
 
Brian Yi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the package to import from for SelectItem in the backing bean?

Thanks,

Sonia
 
ali hammad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Gregg Bolinger Thank you very much. Your code solved my problem. Thanks alot
 
Namita Nair
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This url might be helpful for beginners
http://balusc.blogspot.com/2007/10/populate-child-menus.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic