title: Primefaces 4, dynamic menu setCommand method
body: <p>I'm trying primefaces 4 but there are no documentation around for the new <code>MenuModel</code>. Here, <strong>Optimus Prime</strong> wrote about the new menu system with a little example.
  <a href="http://blog.primefaces.org/?p=2594" rel="noreferrer">http://blog.primefaces.org/?p=2594</a></p>
  
  <p>At this point, he wrote about a <code>setCommand</code> method:</p>
  
  <p><img src="https://i.stack.imgur.com/G505n.png" alt="setCommand"></p>
  
  <p>This point to a <code>save</code> method (found in the pf4 showcase: <a href="http://www.primefaces.org/showcase/ui/menu/menu.xhtml" rel="noreferrer">http://www.primefaces.org/showcase/ui/menu/menu.xhtml</a>):</p>
  
  <p><img src="https://i.stack.imgur.com/MNBaA.png" alt="save method"></p>
  
  <p>After this <strong><em>introduction</em></strong>, 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.</p>
  
  <pre><code>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!!");
          //
      }
  </code></pre>
  
  <p>What I want to do, is to change the <code>spSelected</code> in <code>ServiceProviderBackingBean</code>, like I've done in PF3.5:</p>
  
  <pre><code>&lt;p:dock&gt;
      &lt;c:forEach items="#{serviceProvidersBean.sps}" var="sp"&gt;
          &lt;p:menuitem 
              value="#{sp.spInstanceName}" 
              icon="/images/sps/#{sp.spInstanceId}.png" 
              update=":form:spDetail" &gt;
              &lt;f:setPropertyActionListener 
                  value="#{sp}"   
                  target="#{serviceProvidersBean.spSelected}" /&gt;
          &lt;/p:menuitem&gt;
      &lt;/c:forEach&gt;
  &lt;/p:dock&gt;
  </code></pre>
  
  <p>Any help?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Actually I'm doing this, but I'm looking for a better and cleaner way to achieve this.</p>
  
  <pre><code>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));
      //
  }
  </code></pre>