1

I'm very new in using the Primefaces framework.

My problem is, that I want to call a bean method if the user clicks a button, but the method won't be triggered. Nevertheless, IntelliJ autocompletes the method and I use the bean as item source for another control, that works.

Parts of Index.htmlx

<h:form id="newLunch">
    <h:panelGrid columns="2" style="width: 300px;">
        <h:outputLabel for="initiator" value="Initiator" />
        <p:inputText id="initiator" />

        ...

        <p:button value="save" actionListener="#{indexBean.onSaveLunch}" />
    </h:panelGrid>
</h:form>

IndexBean.java

@ManagedBean
@SessionScoped
public class IndexBean
{
    public IndexBean()
    {
        //...
    }

    public void onSaveLunch()
    {
        // do some action, will not be triggered
    }
}

Thanks for supporting a newbie!

Tobonaut
  • 2,245
  • 2
  • 26
  • 39

2 Answers2

2
    <p:button value="save" actionListener="#{indexBean.onSaveLunch}" />

Use Primefaces CommandButton here.

    <p:commandButton value="save" actionListener="#{indexBean.onSaveLunch}" />

More info here

Updated:

The reason your method is not being called because it is not valid for actionListener.

Change your method from

  public void onSaveLunch()
        {
            // do some action, will not be triggered
        }

to

 public void onSaveLunch(ActionEvent event)
    {
        // do some action, will not be triggered
    }

Remember to import import javax.faces.event.ActionEvent; because there is another java swing action event which you don't want to use.

Makky
  • 17,117
  • 17
  • 63
  • 86
  • Thanks for the fast reply. I tried this before and it didn't work as well. I've retested it just a couple of seconds ago. That's a weird behaviour in my eyes. I know, that is my fault. – Tobonaut May 18 '14 at 14:29
  • In my IDE's output windows, I see following row every time i click the button: `127.0.0.1 - - [18/May/2014:14:30:57 +0000] "POST /base/index.xhtml HTTP/1.1" 200 210 "http://localhost:8080/base/" [...]"` – Tobonaut May 18 '14 at 14:31
  • Thanks @Makkx for your update! But the Luiggi Mendoza's idea was (another working) solution. – Tobonaut May 18 '14 at 20:40
1

You should use the right component and the right attribute. Change <p:button> by <p:commandButton> and use action instead of actionListener:

<p:commandButton value="save" action="#{indexBean.onSaveLunch}" />

If this doesn't work, then there's another problem within your code that you haven't shown. For that, update the question with your current code or check the reasons from here: commandButton/commandLink/ajax action/listener method not invoked or input value not updated

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332