当前位置: 动力学知识库 > 问答 > 编程问答 >

jsf - <f:selectItems is not returning value in <p:selectOneMenu>

问题描述:

I have a problem with getting value of f:selectItems but it is returning its label.

here is the code;

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

here is the dataMigeration class

@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("\n\n\n\n | value:"+mdId);

}

but I am getting mdId=itemLabel of f:selectedItem but I want to get itemValue of f:selectem. SomeWhere I have done before but I forgot now how to get ItemValue.

网友答案:

Your 'f:selectItems' statement is wrong. You can use the 'var' attribute to loop through a list of complex Java objects to dynamically create a list of SelectItem objects. (See http://www.mkyong.com/jsf2/jsf-2-dropdown-box-example/ for examples)

But in this case, you've already created your list of SelectItem objects in the backing bean, so just reference it like this instead:

<f:selectItems value="#{dataMigeration.majorDiseas}" />
分享给朋友:
您可能感兴趣的文章:
随机阅读: