【发布时间】:2012-11-29 05:16:10
【问题描述】:
我正在尝试创建一个动态菜单:类似于在 Amazon 或 eBay 上找到的用于浏览类别的菜单。我的第一次尝试如下所示:
支持 bean:
@ManagedBean
@ViewScoped
public class CategoryBackBean implements ActionListener {
private MenuModel model;
private Category category;
public CategoryBackBean() throws IOException {
category = Category.createRootCategory();
createModel();
}
private void createModel() throws IOException {
MenuModel tempModel = new DefaultMenuModel();
for(Category c : category.getChildCategories()) {
MenuItem childItem = new MenuItem();
childItem.setValue(c.getName());
childItem.addActionListener(this);
tempModel.addMenuItem(childItem);
}
this.model = tempModel;
}
public MenuModel getModel() {
return model;
}
@Override
public void processAction(ActionEvent event) throws AbortProcessingException {
try {
MenuItem item = (MenuItem) event.getSource();
String categoryName = (String) item.getValue();
for(Category c : category.getChildCategories()) {
if(c.getName().equals(categoryName)) {
category = c;
createModel();
return;
}
}
} catch (IOException ex) {
Logger.getLogger(CategoryBackBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
网页:
<h:body>
<h:form>
<p:menubar model="#{categoryBackBean.model}" />
</h:form>
</h:body>
对于初学者,我的设计不起作用:创建了初始菜单,但是当单击按钮时,不会在子类别中重新创建菜单。
解决这个普遍问题的最佳方法是什么?我不是在寻找让上述代码正常工作的快速技巧——我正在寻找递归菜单的通用设计。
【问题讨论】:
-
您应该在
@SessionScoped托管bean 中动态构建您的菜单组件,然后将此菜单绑定到您的<p:menubar>。动态创建 PrimeFaces 组件:How to create Dynamic PrimeFaces menus -
大声笑,这是我自己的博客。我对那个设计有意见。我正在尝试远离
@SessionScoped,并想使用ajax(您链接的设计没有)。 -
我喜欢那个设计。您能否粘贴类别类或接口的内容。谢谢
-
接口将只包含您在此处看到的方法。这将完全取决于上下文。有关模式的详细信息,请参见此处:kevindoran1.blogspot.co.nz/2012/02/…
标签: jsf dynamic menu primefaces