简体   繁体   中英

JSF Add to selectOneMenu from a List<School>

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM