如何从jsf中的selectOneMenu获取所选项目值的id?

时间:2012-06-14 10:38:21

标签: jsf-2

我有类别下拉列表。在选择特定类别时,我已经呈现了该类别的子类别的另一个下拉列表。我想要选择子类别的ID。但我没有得到它。我的代码是:

 <tr>
     <td align="right">
          <h:outputText value="Select Main Category:"></h:outputText>
     </td>
     <td>
          <h:selectOneMenu id="cmbcategory" value="#{categoryBean.categoryID}" required="true" requiredMessage="Select Category" >
               <f:selectItem itemLabel="--Select Category--" itemValue="0"></f:selectItem>
               <f:selectItems value="#{categoryBean.categories}" var="item" itemLabel="#{item.categoryName}" itemValue="#{item.categoryID}"></f:selectItems>
               <f:ajax event="valueChange" render="cmbsubcategory"  immediate="true" listener="#{categoryBean.SubcategoriesByCategory()}"/>
          </h:selectOneMenu>
     </td>
     <td><h:message for="cmbcategory"></h:message></td>
 </tr>
 <tr>
      <td align="right">
          <h:outputText value="Select Sucategory:"></h:outputText>
      </td>
      <td>
           <h:selectOneMenu id="cmbsubcategory" value="#{categoryBean.subCategoryID}" required="true" requiredMessage="Select Subcategory">
                <f:selectItem itemLabel="--Select Subcategory--" itemValue="0"></f:selectItem>
                <f:selectItems value="#{categoryBean.SubcategoriesByCategory()}" var="item" itemLabel="#{item.subcategoryID}" itemValue="#{item.subcategoryID}"></f:selectItems>

           </h:selectOneMenu>
      </td>
      <td><h:message for="cmbsubcategory"></h:message></td>
 </tr>    

我的豆子是:

public int getCategoryID() {
        return CategoryID;
    }

    public void setCategoryID(int CategoryID) {
        this.CategoryID = CategoryID;
    }


    public String getCategoryName() {
        return CategoryName;
    }

    public void setCategoryName(String CategoryName) {
        this.CategoryName = CategoryName;
    }

    public Collection<Category> getCategories() {
        categories=abr.getAllCategory();
        return categories;
    }

    public Collection<Subcategory> SubcategoriesByCategory()
    {
        System.out.print("catid...... "+CategoryID);
        subcats= abr.searchAllSubcategoriesByCategory(CategoryID);

        return subcats;

//            +"size....."+ subcats.size());
     }

    public String getSubCatName() {
        return SubCatName;
    }

    public void setSubCatName(String SubCatName) {
        this.SubCatName = SubCatName;
    }

    public int getSubCategoryID() {
        System.out.print("subcatid...... "+SubCategoryID);
        return SubCategoryID;
    }

    public void setSubCategoryID(int SubCategoryID) {
        this.SubCategoryID = SubCategoryID;
    }

    public Collection<Subcategory> getSubcats() {
        return subcats;
    }

    public void setSubcats(Collection<Subcategory> subcats) {
        this.subcats = subcats;
    }



    public void setCategories(Collection<Category> categories) {
        this.categories = categories;
    }

1 个答案:

答案 0 :(得分:0)

您正在使用操作方法混合属性getter。 getter应返回该属性,操作方法应为void或返回String

更改

public Collection<Subcategory> SubcategoriesByCategory() {
  ..
}

public void searchSubcats() {
  subcats= abr.searchAllSubcategoriesByCategory(CategoryID);
}

并在你的ajax请求中调用此方法:

<f:ajax .. listener="#{categoryBean.searchSubcats}"/>

第二个drow-down中的selectItem然后需要引用子类的getter:

<f:selectItems value="#{categoryBean.subcats}" ... />