Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jsf - Primefaces 4, dynamic menu setCommand method

I'm trying primefaces 4 but there are no documentation around for the new MenuModel. Here, Optimus Prime wrote about the new menu system with a little example. http://blog.primefaces.org/?p=2594

At this point, he wrote about a setCommand method:

setCommand

This point to a save method (found in the pf4 showcase: http://www.primefaces.org/showcase/ui/menu/menu.xhtml):

save method

After this introduction, here's the question/problem. I'm creating a dynamic menu from a bean but I don't understand how know the menu clicked by the user and do the right operation.

public void init() {
        if (spBean == null) {
            System.out.println("spBean is NULL!");
            return;
        }
        for (ServiceProvider sp: spBean.getListaSP()) {
            DefaultMenuItem item = new DefaultMenuItem(sp.getNome());
            //item.setUrl("#");
            item.setIcon("images/sps/" + sp.getImageId() + ".png");
            item.setCommand("#{dockMenuBackingBean.setNewMenu}");
            //
            model.addElement(item);
            System.out.println(sp.getNome());
        }
    }

    public void setNewMenu() {
        System.out.println("A menu was clicked BUT witch menu? Arghh!!");
        //
    }

What I want to do, is to change the spSelected in ServiceProviderBackingBean, like I've done in PF3.5:

<p:dock>
    <c:forEach items="#{serviceProvidersBean.sps}" var="sp">
        <p:menuitem 
            value="#{sp.spInstanceName}" 
            icon="/images/sps/#{sp.spInstanceId}.png" 
            update=":form:spDetail" >
            <f:setPropertyActionListener 
                value="#{sp}"   
                target="#{serviceProvidersBean.spSelected}" />
        </p:menuitem>
    </c:forEach>
</p:dock>

Any help?

EDIT:

Actually I'm doing this, but I'm looking for a better and cleaner way to achieve this.

public void init() {
    if (spBean == null) {
        System.out.println("spBean is NULL!");
        return;
    }
    for (ServiceProvider sp: spBean.getListaSP()) {
        DefaultMenuItem item = new DefaultMenuItem(sp.getNome());
        //item.setUrl("#");
        item.setIcon("images/sps/" + sp.getImageId() + ".png");
        String command = String.format("#{dockMenuBackingBean.setNewMenu('%d')}", spBean.getListaSP().indexOf(sp));
        item.setCommand(command);
        //
        model.addElement(item);
        System.out.println(sp.getNome());
    }
}

public void setNewMenu(Object x) {
    Integer selectedId = Integer.parseInt((String)x);
    System.out.println("Menu changed " + Integer.toString(selectedId));
    //
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Setting command parameters by setParam(key,value) can be done like that:

In your menu generating bean:

DefaultMenuItem item = new DefaultMenuItem("display list");
item.setId("listMenuItem");
item.setCommand("#{myBean.displayList}");
item.setParam("listId", 1l);

In your managed bean containing the action:

public String displayList(ActionEvent event) {
    MenuItem menuItem = ((MenuActionEvent) event).getMenuItem();
    Long id = Long.parseLong(menuItem.getParams().get("listId").get(0));
    findListBy(id);
}

Reading parameters seems to be a bit complicated. But ActionListeners aren't supported by Primefaces 4 MenuItems (because they aren't derived from UICommand any more) so params seem to be new new way.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...