java JSF 从 List<School> 添加到 selectOneMenu

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6886000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:45:06  来源:igfitidea点击:

JSF Add to selectOneMenu from a List<School>

javajsfnetbeansglassfish-3selectonemenu

提问by Illep

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

我有一个 List 对象,其中包含 School.Id、School.Name 和 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.

我需要在 selectOneMenu 列表框中列出所有 School.Name。Java 代码和相应的 JSF 代码将如何。

My workings so far;

到目前为止我的工作;

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

Java Class

Java类

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

findAllSchool()方法实际上返回一个List<School>对象。

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

我需要在列表框(下拉列表/ selectOneMenu )上显示这些学校名称。我怎样才能做到这一点 ?

  • I am using Netbeans 6.9.1 and Galssfish 3
  • 我正在使用 Netbeans 6.9.1 和 Galssfish 3

回答by Julien Lafont

You can use the var, itemLabeland itemValueattributes :

您可以使用var,itemLabelitemValue属性:

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

根据您在 School 类中有一个 getId() 和 getName() ,在您的托管 bean 中有一个 getSelectedSchool() 和 setSelectedSchool(School s) 。