You may try the below piece of code: Hope this will help you-
The data of a menu item is represented by the MenuItem class
public class MenuItem {
private int id;
private String label;
public MenuItem(String label, int id) {
super();
this.label = label;
this.id = id;
}
public int getId() {
return this.id;
}
public String getLabel() {
return this.label;
}
public void setId(int id) {
this.id = id;
}
public void setLabel(String label) {
this.label = label;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("MenuItem [id=");
builder.append(this.id);
builder.append(", label=");
builder.append(this.label);
builder.append("]");
return builder.toString();
}
}
The dynamic menu is supported by the DynamicMenu class. It provides the list of menu items and an action method:
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.jboss.seam.annotations.Name;
@Name("dynMenu")
public class DynamicMenu {
private Logger log = Logger.getLogger(DynamicMenu.class.getName());
public void action(int id) {
log.info("Action called with menu item id: " + id);
}
public List<menuitem> getMenuItems() {
List<menuitem> menuItems = new ArrayList<menuitem>();
menuItems.add(new MenuItem("Menu Item #1", 1));
menuItems.add(new MenuItem("Menu Item #2", 2));
menuItems.add(new MenuItem("Menu Item #3", 3));
return menuItems;
}
}
The following code snippet contains the dynamic menu xhtml example. The key in the dynamic menu items is the <c:forEach> iterator. The namespace declaration is very important, it should be xmlns:c="http://java.sun.com/jstl/core". If you use xmlns:c="http://java.sun.com/jsp/jstl/core" namespace, the iterator won't work!
<h:form xmlns:h="http://java.sun.com/jsf/html">
<rich:dropDownMenu value="Dynamic Menu Item Example" style="text-decoration:none;">
<c:forEach xmlns:c="http://java.sun.com/jstl/core" var="item"
items="#{dynMenu.getMenuItems()}">
<rich:menuItem id="menuItem#{item.id}" submitMode="ajax"
value="#{item.label}" action="#{dynMenu.action(item.id)}">
</rich:menuItem>
</c:forEach>
</rich:dropDownMenu>
</h:form>