从Primefaces对话框调用Java方法

时间:2017-06-27 21:26:32

标签: jsf primefaces jsf-2

我想从Primefaces对话框中调用Java方法。我测试了这段代码:

<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());
    }

}

我点击按钮没有任何反应。你能就我如何解决这个问题提出一些建议吗?

1 个答案:

答案 0 :(得分:2)

您应该删除type="button"中的commandButton,因为这会阻止按钮发送请求。

此外,您在actionListener中使用commandButton bean中的方法应该以{{1​​}}作为参数。

ActionEvent

有关其他信息,请参阅here