Kalle Richter
Kalle Richter

Reputation: 8728

How to specify a backing bean callback on click on a menu item?

I'm building the content of a p:slideMenu by binding the value to a MenuModel in a backing bean. This is necessary because the content is generated dynamically based on the result of a database query. Using

@Named
@ViewScoped
public class BackingBeanView0 implements Serializable {
    private static final long serialVersionUID = 1L;
    private MenuModel menuModel = new DynamicMenuModel();

    @PostConstruct
    private void init() {
        DefaultMenuItem menuItem = new DefaultMenuItem("Click me!",
                null, //icon
                "/index.xhtml" //url
        );
        menuItem.setCommand("#{backingBeanView0.onMenuItemClick('Hello world!')}");
        menuItem.setImmediate(true);
        menuModel.addElement(menuItem);
    }

    [getter and setter for menuModel]

    public void onMenuItemClick(String message) {
        System.out.println(BackingBeanView0.class.getName()+" message: "+message);
    }
}

as recommended by @Melloware (this does not show the need to create the model in the backing bean) causes backingBeanView0.onMenuItemClick to be not invoked

to be displayed with a delay for a few seconds. Moving the wanted backing bean method to a view scoped bean doesn't change this behavior.

The onXXX properties for Javascript callbacks on DefaultMenuItem can't be used to trigger a method in a backing bean afaik. I noticed that the command property in DefaultMenuItem isn't used in the Primefaces source code and it is not documented in the Primefaces 6.2 user guide.

I'm providing a SSCCE at https://gitlab.com/krichter/primefaces-menuitem-bean-callback. It doesn't contain more information than the MCVE above and merely exists to ease the investigation of the problem.

I'm using Primefaces 6.2.

Upvotes: 2

Views: 1049

Answers (1)

Melloware
Melloware

Reputation: 12019

I think I know what you are asking. In the example below, I call the bean controller method myController.changeAccount and I also provide an OnComplete Javascript callback as if I built the menu in XHTML.

final DefaultMenuItem item = new DefaultMenuItem(bean.getLongName());
item.setCommand("#{myController.changeAccount('" + bean.getShortName() + "')}");
item.setImmediate(true);
item.setOncomplete("melloware.handleAccountChange(xhr, status, args);");

Change:

DefaultMenuItem menuItem = new DefaultMenuItem("Click me!",
                null, //icon
                "/index.xhtml" //url
        );

To:

DefaultMenuItem menuItem = new DefaultMenuItem("Click me!");

You cannot combine a "URL" parameter and an Action command in the same menuitem it uses the URL first. If you need to send to a new location have your Command simply return that as a string and you will navigate to that page for example:

public String onMenuItemClick(String message) {
      System.out.println(BackingBeanView0.class.getName()+" message: "+message);
      return "/index.xhtml";
}

Upvotes: 2

Related Questions