2

I have a List object which hold School.Id, School.Name, and School.Address .

I need to list all the School.Name in a selectOneMenu List box. How will be the Java code and the corresponding JSF code will be.

My workings so far;

<h:selectOneMenu value="#{School.listschoolName}">
<f:selectItems value="#{School.listschoolName}" />
</h:selectOneMenu>

Java Class

//And also i got the corresponding getters and setters for these
private List<School> listschool; 

public void listschoolName(){
    setListschool(hml.findAllSchool());
}

The findAllSchool() method actually returns a List<School> object.

I need to display these School Names on a List Box (dropdown / selectOneMenu ). How can i do this ?

  • I am using Netbeans 6.9.1 and Galssfish 3
emre nevayeshirazi
  • 18,983
  • 12
  • 64
  • 81
Illep
  • 16,375
  • 46
  • 171
  • 302

1 Answers1

7

You can use the var, itemLabel and itemValue attributes :

<h:selectOneMenu value="#{bean.selectedSchool}">
    <f:selectItems value="#{School.listschoolName}" var="_school" itemValue="#{_school.id}" itemLabel="#{_school.name}"/>
</h:selectOneMenu>    

According you have a getId() and getName() in the School class, and a getSelectedSchool() and setSelectedSchool(School s) in your managed bean.

Julien Lafont
  • 7,869
  • 2
  • 32
  • 52
  • 3
    The item value becomes the selected value. You've set the item value to be School ID. However the setter expects a School. So your answer is not exactly going to work :) See also http://stackoverflow.com/questions/6848970/how-to-prepopulate-a-hselectonemenu-from-a-db – BalusC Jul 31 '11 at 01:19
  • Yes, I've forgot the Convertor. Thank – Julien Lafont Jul 31 '11 at 11:07