0

我有类别的下拉列表。在选择特定类别时,我呈现了该类别子类别的另一个下拉列表。我想要选定子类别的 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;
    }
4

1 回答 1

0

您正在将属性获取器与操作方法混为一谈。getter 应该返回属性,而 action 方法应该是void或返回一个String.

改变

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

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

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

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

然后,第二个下拉列表中的 selectItems 需要引用子猫的 getter:

<f:selectItems value="#{categoryBean.subcats}" ... />
于 2012-06-14T11:49:22.347 回答