在ui中使用动态p:菜单:重复不起作用

时间:2014-04-26 19:06:42

标签: jsf primefaces

这是场景(简化): 我想在ui中使用动态p:菜单:重复如下所示...

<h:form>
   <ui:repeat value="#{bloggerBean.listOfModel}" var="categoryModel" varStatus="loop">
       <p:commandButton id="dynaButton" type="button" 
             value="#{categoryModel.category.name}"/> 
       <p:menu model="#{categoryModel}" overlay="true" trigger="dynaButton" 
                    my="left top" at="left bottom" />
   </ui:repeat>
</h:form>

有了这个,按钮和menuitems显示正确但是只有循环中的最后一个p:menu动作侦听器正常工作(在托管bean处调用)。显然是因为我没有将varStatus用于模型。 如果我将上面的内容更改为使用varStatus,如下所示,

<h:form>
   <ui:repeat value="#{bloggerBean.listOfModel}" var="categoryModel" varStatus="loop">
       <p:commandButton id="dynaButton" type="button" 
             value="#{categoryModel.category.name}"/> 
       <p:menu model="#{bloggerBean.listOfModel[loop.index]}" overlay="true" trigger="dynaButton" 
                    my="left top" at="left bottom" />
   </ui:repeat>
</h:form>

显示按钮和菜单,但每个p:菜单中的menuItem都为空。

这类似于BalusC报道[这里](Using <ui:repeat><h:inputText> on a List<String> doesn't update model values)!

知道为什么吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

ui:重复无法将选择事件附加到p:菜单菜单项,但面包屑模型中的最后一个p:菜单除外。我使用primefaces remoteCommand来解决这个问题。这是我改变的...我在我的模型中为每个menuItems设置ActionListener(同时生成动态面包屑),如下所示:

public class CategoryMenuModel implements MenuModel, ActionListener, Serializable {
...
for(Category childCategory : category.getChildCategories()) {
   MenuItem subCategoryItem = new MenuItem();
   ...
   subCategoryItem.addActionListener(this);

}  
...
}

这不像我在问题中所讨论的那样工作,所以我更改了代码如下

public class CategoryMenuModel implements MenuModel, ActionListener, Serializable {
...
for(Category childCategory : category.getChildCategories()) {
   MenuItem subCategoryItem = new MenuItem();
   ...

   //Since ui:repeat isn't attaching events to submenus except the last p:menu
   //use instead javascript, pass whatever info as a name value pair
   String parentMapping = "{name: 'parentName', value: '" + category.getName() + "'}";
   String subCategoryMapping = "{name: 'submenuName', value: '" + childCategory.getName() + "'}";
   String remoteMethod = "handleSubmenuSelection([" + parentMapping + "," + subCategoryMapping + "])";
   subCategoryItem.setOnclick(remoteMethod);

}  
...
}

在我的.xhtml中,我在所有面包屑子菜单中添加了p:remoteCommand listen menuItems click事件

<p:remoteCommand name="handleSubmenuSelection" action="#{bloggerBean.processSubmenuSelection()}" />

最后,我在托管bean解析&#39; parentName&#39;中添加了一些代码。和&#39;子菜单名称&#39;并且处理menuItem从我的模型中选择哪个面包屑如下:

@ManagedBean
@ViewScoped
public class BloggerBean {
...
public void processSubmenuSelection() {
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, String> map = context.getExternalContext().getRequestParameterMap();
    this.parentName = map.get("parentName");
    this.submenuName = map.get("submenuName");
    for (CategoryMenuModel model : listOfModel) {
        if(parentName.equals(model.category.getName())){
            model.processSubmenuSelection(submenuName);
            break;
        }
    }
}
...
}

在我的模特中

public class CategoryMenuModel implements MenuModel, ActionListener, Serializable {
...
void processSubmenuSelection(String submenuName) {
    System.out.println("In processSubmenuSelection ... starting");
    for (Category childCategory : category.getChildCategories()) {
        if(childCategory.getName().equals(submenuName)){
            setCategory(childCategory);
            break;
        }
    }
}
...
}

希望,这可以帮助有类似问题的人。