0

I want to call Java method from Primefaces dialog. I tested this code:

<h:form>

                        <p:dialog header="New Sensor" widgetVar="dlg" focus="name" modal="true" showEffect="fade">
                            <h:panelGrid columns="2" cellpadding="5">
                                <h:outputLabel for="name" value="Name" />
                                ........
                                <p:inputText id="enabled" label="enabled" value="#{newSensor.sensor.enabled}" />
                            </h:panelGrid>

                            <f:facet name="footer">
                                <p:commandButton id="ajax" value="Create Sensor" styleClass="ui-priority-primary" type="button" actionListener="#{newSensor.saveRecord()}"/>
                            </f:facet>
                        </p:dialog>

                    </h:form>

Java bean:

@Named
@RequestScoped
public class NewSensor implements Serializable
{
    private SensorObj sensor = new SensorObj();

    public SensorObj getSensor()
    {
        return sensor;
    }

    public void setSensor(SensorObj sensor)
    {
        this.sensor = sensor;
    }

    public void saveRecord(){
        System.out.println(">>>>>>>!!!!!! " + sensor.getName());
    }

}

Wehn I click the button nothing happens. Can you give some advice how I can fix this issue?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • sometimes adding attribute process="@this" fixes my problem – Fritz Jun 28 '17 at 05:48
  • did you see this post [Update from dialog](https://stackoverflow.com/questions/44698063/how-to-reset-inputtext-field-in-jsf-primefaces/44698531#44698531) ?!? (see the third solution) – Yagami Light Jun 28 '17 at 07:50
  • Possible duplicate of [commandButton/commandLink/ajax action/listener method not invoked or input value not updated](https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value) – Kukeltje Jul 05 '17 at 21:28
  • @YagamiLight: #3 is a very weird solution... Very... Why not just remove the `type="button"`??? – Kukeltje Jul 05 '17 at 21:29

1 Answers1

2

You should remove type="button" in your commandButton because it will prevent the button from sending a request.

Additionally, you are using actionListener in your commandButton.
Your method in the bean should have ActionEvent as its parameter.

public void saveRecord(ActionEvent actionEvent) {
    System.out.println(">>>>>>>!!!!!! " + sensor.getName());
}

Please refer here for additional information.

Tonkichi
  • 251
  • 2
  • 7
  • No need to answer questions by Peter Tonev, you rarely get useful feedback. But your comment about the ActionEvent is wrong. It is valid if the actionListener contained EL without () at the end, but since they are there the ActionEvent can be omitted. And marking it as a duplicate of https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value is allowed. This is #1 in that Question – Kukeltje Jul 05 '17 at 21:27
  • @Kukeltje Thank you for your feedback. Also, thank you for your clarification about ActionListener. I overlooked the () at the end of the EL. – Tonkichi Jul 06 '17 at 00:25