Kev*_*vin 3 jsf menu dynamic primefaces
我正在尝试创建一个动态菜单:像亚马逊或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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
网页:
<h:body>
<h:form>
<p:menubar model="#{categoryBackBean.model}" />
</h:form>
</h:body>
Run Code Online (Sandbox Code Playgroud)
对于初学者,我的设计不起作用:创建了初始菜单,但是当单击按钮时,菜单不会在子类别中重新创建.
解决这个普遍问题的最佳方法是什么?我不是在寻找快速的黑客来让上面的代码工作 - 我正在寻找一个递归菜单的通用设计.
操作完成后,您需要更新菜单.鉴于您不想对菜单ID进行硬编码,请使用@parent
.
childItem.setUpdate("@parent");
Run Code Online (Sandbox Code Playgroud)
如果菜单是表单中的唯一组件,则另一种方法是使用@form
.
这一切是否是"最佳"方式都无法客观地回答.如果代码以最简单和最少侵入的方式完成您想要的工作,那么它是可以接受的.