私は動的メニューを作成しようとしています。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>
手始めに、私のデザインは機能しません。最初のメニューは作成されますが、ボタンをクリックしても、メニューはサブカテゴリに再作成されません。
この一般的な問題に取り組むための最良の方法は何ですか?上記のコードを機能させるための簡単なハックは探していません。再帰メニューの一般的なデザインを探しています。