Primefaces p:commandButton with action not called

user1740789 picture user1740789 · Oct 12, 2012 · Viewed 51.9k times · Source

I've got some some troubles with Primefaces 3.2 and JSF 2.1.

My Code like this:

<p:toolbar id="jeditortoolbar" styleClass="jeditortoolbar">
      <p:toolbarGroup align="left" height="25" style="height:25px">
        <p:commandButton type="button" title="#{msg.beenden}"/>
        <p:commandButton type="button" title="#{msg.neu}"/>
      </p:toolbarGroup>
</p:toolbar>

When I take a look at Primefaces Showcase my p:commandButton need

actionListener="#{myBean.myActionMethod}"

and my Bean need a Method like

public void myActionMethod(){}

I have a h:form around my p:toolbar tag!

My Bean is ViewScoped.

My Workaround is In *.xhtml File

<p:commandButton type="button" title="#{msg.neu}" onclick="addNewEmptyFile()"/>
<p:remoteCommand name="addNewEmptyFile" update=":codeTabForm">
   <f:setPropertyActionListener value="#{true}" target="#{myBean.myEvent}"/>
</p:remoteCommand>

In MyBean.java

private String myEvent;

public void setMyEvent(String value){ myActionMethod();}

This works for me but I think this is very dirty code.

Can everyone help me?

Answer

Kerem Baydoğan picture Kerem Baydoğan · Oct 12, 2012

Try this

Bean.java

@ManagedBean
@ViewScoped
public class Bean {

    public String testButtonAction() {
        System.out.println("testButtonAction invoked");
        return "anotherPage.xhtml";
    }

    public void testButtonActionListener(ActionEvent event) {
        System.out.println("testButtonActionListener invoked");
    }

}

page.xhtml

<p:toolbar>
  <p:toolbarGroup>
    <p:commandButton action="#{bean.testButtonAction}"/>
    <p:commandButton actionListener="#{bean.testButtonActionListener}"/>
  </p:toolbarGroup>
</p:toolbar>