xuanhung2401
xuanhung2401

Reputation: 311

Programmatic Menu with parameter?

Using primefaces 2.2.1. We have MenuBean class :

public class MenuBean {

private MenuModel model;

public MenuBean() {
    model = new DefaultMenuModel();

    //First submenu
    Submenu submenu = new Submenu();
    submenu.setLabel("Dynamic Submenu 1");

    MenuItem item = new MenuItem();
    item.setValue("Dynamic Menuitem 1.1");
    item.setUrl("#");
    submenu.getChildren().add(item);

    model.addSubmenu(submenu);

}

public MenuModel getModel() {
    return model;
}   
    }

This is view :

 <h3>Programmatic Menu </h3>
 <p:menu model="#{menuBean.model}"/>    

I want to call Controller method with parameter, something like :

 <p:menuitem value="Menu 01" action="#{myController.search()}">
      <f:param value="1" name="mypara"/>
  </p:menuitem> 

I try : item.addActionListener("#{myController.search()}"); but it's not work. What can i do in MenuBean class ?

Upvotes: 1

Views: 2516

Answers (2)

ltlBeBoy
ltlBeBoy

Reputation: 1312

With current PrimeFaces versions (v6.x) a solution could look like this:

MenuBean:

@Named
public class MenuBean implements Serializable {

    private MenuModel model;

    @PostConstruct
    public void init() {
        model = new DefaultMenuModel();

        //First submenu
        Submenu submenu = new Submenu();
        submenu.setLabel("Dynamic Submenu 1");

        MenuItem item = new MenuItem("Dynamic Menuitem 1.1");
        item.setCommand("#{myController.search}"); // note missing braces when calling "search" method (action event is added by JSF)!
        item.setParam("mypara", 1); // set paramater and its value (e.g. integer value)
        item.setProcess("@this"); // process this command and its paramerter only
        item.setUpdate("@form"); // update the current form (or maybe different component ID)
        submenu.getChildren().add(item);

        model.addSubmenu(submenu);
    }

    public MenuModel getModel() {
        return model;
    }

}

MyController:

@Named
public class MyController implements Serializable {

    public void search(MenuActionEvent menuActionEvent) {
        // extract the parameter
        int myparaValue = Integer.parseInt(menuActionEvent.getMenuItem()
            .getParams()
            .get("mypara") // better use constant for parameter name!
            .get(0)); // note that parameter contains a list of strings!

        // do stuff with parameter value...
    }

}

Upvotes: 3

Darkaico
Darkaico

Reputation: 1256

Are you using Spring? Because the integration of that use JSF 1.2 and if you want to send a paramter to a controller you have to made a kind of workaround.

I had the same problem and I resolve as the following lines:

<p:menuitem value="Menu 01" action="#{myController.search}">
    <f:setPropertyActionListener target="#{managedBean.myparam}" value="1" />
</p:menuitem>

in your case you could try to add a property into your MenuModel. Something like that:

public class MenuBean {

...

private int myParam;

public int getMyParam() {
    return myParam;
}

public void setMyParam(int myParam) {
    this.myParam = myParam;
}

public void search() {
    //here you could use the param sending by the view
    service.find(myParam);
}

...
}

Upvotes: 1

Related Questions