【问题标题】:How to specify a backing bean callback on click on a menu item?如何在单击菜单项时指定支持 bean 回调?
【发布时间】:2018-07-30 11:25:46
【问题描述】:

我通过将值绑定到支持 bean 中的 MenuModel 来构建 p:slideMenu 的内容。这是必要的,因为内容是根据数据库查询的结果动态生成的。使用

@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);
    }
}

按照@Melloware 的建议(这并不表明需要在支持 bean 中创建模型)导致backingBeanView0.onMenuItemClick调用

延迟显示几秒钟。将所需的支持 bean 方法移动到视图范围的 bean 不会改变这种行为。

DefaultMenuItem 上的 Javascript 回调的 onXXX 属性不能用于触发支持 bean afaik 中的方法。我注意到 DefaultMenuItem 中的 command 属性未在 Primefaces 源代码中使用,并且未在 Primefaces 6.2 用户指南中记录。

我在 https://gitlab.com/krichter/primefaces-menuitem-bean-callback 提供 SSCCE。它不包含比上述 MCVE 更多的信息,只是为了便于调查问题而存在。

我正在使用 Primefaces 6.2。

【问题讨论】:

    标签: jsf primefaces menu callback menu-items


    【解决方案1】:

    我想我知道你在问什么。在下面的示例中,我调用了 bean 控制器方法 myController.changeAccount,并且还提供了一个 OnComplete Javascript 回调,就好像我在 XHTML 中构建了菜单一样。

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

    更改:

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

    收件人:

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

    您不能将“URL”参数和操作命令组合在它首先使用 URL 的同一个菜单项中。如果您需要发送到一个新位置,您的命令只需将其作为字符串返回,您将导航到该页面,例如:

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

    【讨论】:

      猜你喜欢
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      相关资源
      最近更新 更多