Pages

Wednesday, December 19, 2007

Dynamic jsf menu

In this blog I will explain how you can create a dynamic menu in jsf pages from a bean. The data of the menu can for example come from a xml or from a database table. In my other blog I use adf taskflow with a xml menu, this time I use menu items.

First we create a taskflow with two views and with two control flow cases

Now we can create a jsf template with the menubar where we add our menu items.

We create the menu bean and defined it as a request managed bean in the adfc-config.xml

private RichMenuBar initMenu;

public void createMenus(PhaseEvent phaseEvent) {

boolean addMenu = true;
for (Iterator iterator = initMenu.getChildren().iterator(); iterator.hasNext();) {
UIComponent component = (UIComponent) iterator.next();
if ( component.getId().equalsIgnoreCase("menuMain")){
addMenu = false;
}
}
if (addMenu) {
//Create Menu and set id and text
RichMenu menuMain = new RichMenu();
menuMain.setId("menuMain");
menuMain.setText("Main");

RichCommandMenuItem menuPage1 = new RichCommandMenuItem();
menuPage1.setId("page1");
menuPage1.setText("Page 1");
menuPage1.setActionExpression(getMethodExpression("page1"));

RichCommandMenuItem menuPage2 = new RichCommandMenuItem();
menuPage2.setId("page2");
menuPage2.setText("Page 2");
menuPage2.setActionExpression(getMethodExpression("page2"));

menuMain.getChildren().add(menuPage1);
menuMain.getChildren().add(menuPage2);
initMenu.getChildren().add(menuMain);
}
}
private MethodExpression getMethodExpression(String name) {
Class [] argtypes = new Class[1];
argtypes[0] = ActionEvent.class;
FacesContext facesCtx = FacesContext.getCurrentInstance();
Application app = facesCtx.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesCtx.getELContext();
return elFactory.createMethodExpression(elContext,name,null,argtypes);
}


private ValueExpression getValueExpression(String name) {
FacesContext facesCtx = FacesContext.getCurrentInstance();
Application app = facesCtx.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesCtx.getELContext();
return elFactory.createValueExpression(elContext, name, Object.class);
}

We are ready to create the two pages and we only have to add beforePhase attribute to f:view element.
And we are finished. Here is the 11g example project

10 comments:

  1. Hi
    I want to develop dynamic menu according to User Role. Display menu change according to login user. How can I implement this using Jdeveloper 11G

    ReplyDelete
  2. Hi, If I have the time I will make a new example based on the security Roles.

    thanks Edwin

    ReplyDelete
  3. Hi, I converted the application to 11g , and I get a error msg:

    Target Unreachable, identifier 'Menu' resolved to null

    It seems the Menu bean haven't been instantiated yet.

    Do you know what might have gone wrong?

    ReplyDelete
  4. Hi Please use the new menu example which works perfectly in jdev 11g production

    http://biemond.blogspot.com/2008/11/dynamic-menu-based-on-roles-database.html

    ReplyDelete
  5. Hi,
    Thank You for your blogs.
    I create a menu bar and i added menu component in the pagetemplate,
    is there a way to put some code in actionlistener or some place to call the page .
    Thank you1

    David Nahmias

    ReplyDelete
  6. Hi,

    You can call a managed bean with a method or add an action to a menu item.

    or else explain me more what you did

    ReplyDelete
  7. Would really like to see an example of a dynamic menu based on a database table.

    ReplyDelete
    Replies
    1. Hi,

      and here it is http://biemond.blogspot.com/2008/11/dynamic-menu-based-on-roles-database.html
      hope you like it.

      thanks

      Delete
  8. Hi,
    Thank you for you effort,

    I create the menu , and its work fine.
    but how i can highlight the link when it clicked?

    Thnaks and regards,

    ReplyDelete
    Replies
    1. Hi,

      maybe add an EL expression in the styleclass attribute and read a managed bean what is the current menu item or page and then set or unset the selected css class

      Thanks

      Delete