• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSF drop down.

 
Ranch Hand
Posts: 455
  • Number of slices to send:
    Optional 'thank-you' note:
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
 
Ranch Hand
Posts: 119
  • Number of slices to send:
    Optional 'thank-you' note:

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
  • Number of slices to send:
    Optional 'thank-you' note:
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
 
Ranch Hand
Posts: 15304
6
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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
Ranch Hand
Posts: 15304
6
  • Number of slices to send:
    Optional 'thank-you' note:

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
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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
Ranch Hand
Posts: 15304
6
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Greenhorn
Posts: 18
  • Number of slices to send:
    Optional 'thank-you' note:
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
 
Ranch Hand
Posts: 672
  • Number of slices to send:
    Optional 'thank-you' note:
Good question.
"How can I retrieve the item that the user selected from the drop down??"
 
Ranch Hand
Posts: 38
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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
 
Greenhorn
Posts: 22
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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
 
Greenhorn
Posts: 12
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
Got it, silly question..
:p
 
Greenhorn
Posts: 1
  • Number of slices to send:
    Optional 'thank-you' note:

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
 
Ranch Hand
Posts: 137
  • Number of slices to send:
    Optional 'thank-you' note:
What is the package to import from for SelectItem in the backing bean?

Thanks,

Sonia
 
Greenhorn
Posts: 5
  • Number of slices to send:
    Optional 'thank-you' note:
@Gregg Bolinger Thank you very much. Your code solved my problem. Thanks alot
 
Greenhorn
Posts: 3
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Greenhorn
Posts: 2
  • Number of slices to send:
    Optional 'thank-you' note:

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
  • Number of slices to send:
    Optional 'thank-you' note:
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
 
Greenhorn
Posts: 2
  • Number of slices to send:
    Optional 'thank-you' note:
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

 
Greenhorn
Posts: 6
  • Number of slices to send:
    Optional 'thank-you' note:
This url might be helpful for beginners
http://balusc.blogspot.com/2007/10/populate-child-menus.html
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |