我在获取f:selectItems的值时遇到问题,但它正在返回其标签
这是代码;
<p:selectOneMenu>.
<p:outputLabel value="Major Diseases"></p:outputLabel>
<p:selectOneMenu value="#{dataMigeration.mdId}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems var="t"
value="#{dataMigeration.majorDiseas}"
itemLabel="#{t.value.mdName}"
itemValue="#{t.value}"/>
<p:ajax listener="#{dataMigeration.getSubDiseasesByMojarDisease(dataMigeration.mdId)}"
event="change" update="dataMigration"/>
</p:selectOneMenu>
这是数据迁移类
@ManagedBean(name="dataMigeration")
@SessionScoped
public class DataMigeration{
String mdId;
private List<SelectItem> majorDiseas = new ArrayList<SelectItem>();
public List<SelectItem> getMajorDiseas() {
majorDiseas = new ArrayList<SelectItem>();
System.out.println("DataMigeration.getMajorDiseas():");
List<DiseaseCategory> majorDiseasesList = new ArrayList<DiseaseCategory>();
majorDiseasesList=DataManager.findAllRecords();
for (DiseaseCategory obj : majorDiseasesList) {
majorDiseas.add(new SelectItem(obj.getMdId(), obj.getMdName()));
}
return majorDiseas;
}
public void getSubDiseasesByMojarDisease(String mdId)
{
System.out.println("nnnn | value:"+mdId);
}
但我得到的mdId=itemLabel为f:selectedItem,但我想得到f:selectemValue。SomeWhere我以前做过,但现在我忘记了如何获取ItemValue。
您的"f:selectItems"语句是错误的。您可以使用"var"属性在复杂Java对象列表中循环,以动态创建SelectItem对象列表。(请参见http://www.mkyong.com/jsf2/jsf-2-dropdown-box-example/例如)
但在这种情况下,您已经在backingbean中创建了SelectItem对象列表,所以只需像这样引用它:
<f:selectItems value="#{dataMigeration.majorDiseas}" />