Creating rich:dropDownMenu dynamically

RichFaces comes with a drop down menu component. If you need to create the menu items dynamically and try using a4j:repeat, that will not work.



It doesn’t work because rich:dropDownMenu expects rich:menuItem as its children and thus just ignores a4j:repeat. The solution is to use c:forEach instead. When using c:forEach, the loop will be executed during compile time and all the rich:menuItems will be present in the JSF component tree.


The managed bean looks like this:

public class DropDownMenuBean {

   private String [] menu = new String[]{"New", "Open File...", "Save", "Save As...", "Save All", "Exit"};

   public String[] getItems() {
	return menu;

   public DropDownMenuBean() {}

One last thing, for JSTL namespace use this:


16 thoughts on “Creating rich:dropDownMenu dynamically

  1. Nice article (if you do not know that you need to c:forEach to create a list of rich:menuItem from a list in you managed bean)

    But I was hoping for something a little more complex (and a lot more common) How to deal with a managed bean that returns a “tree like” structure? AFAIK c:forEach would be insufficient since it can not deal with recursion.

  2. You probably would have to convert the “tree like” structure to an object understood by c:forEach. Another option is to build the menu programmaticly, inside the managed bean.

  3. the code doesnt work.. when we click on the main menu ie File, then empty dropdown appears. the contents ie New”, “Open File…”, “Save doesnt appear .

    i have used the following

  4. Hi Max,

    I am using a property on the var to create the id for the menu item, like this:

    I have a problem with duplicate component ids when I use a modal pane to add an item to the #{dropDownMenuBean.items} list and rerender the page. Do you know where I might look to solve this issue. I have been looking at it for quite some time, but can’t seem to find a solution.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.