Anton Styopin
Anton Styopin

Reputation: 753

p:ajax fires twice

concerning p:ajax of PrimeFaces. I have two p:ajax requests within my <p:commandButton>. I did stdout within the second method so that I can see if its triggered or not. The same (last) method is triggered twice and I don't know why. If I put only one <p:ajax> request into my button he fires once and if I put two <p:ajax> requests he fires twice.

<h:body style="background:#f5f5f5;">
    <h:form id="formG">
        <p:commandButton styleClass="viewButton" icon="ui-icon-search"
            value="#{msg['button.open']}" id="auftragButtonG">

            <p:ajax listener="#{auftragBean.saveIdIntoAppScope()}"  partialSubmit="true"/>
            <p:ajax listener="#{auftragBean.loadXMLData()}" partialSubmit="true"/>
        </p:commandButton>
    </h:form>
</h:body>
public void loadXMLData(){
    setXmlData(entireXmlData(mandantId(), getJobId()));
    System.out.println(getXmlData().size());
}

Anybody knows why and how I can ignore the second load?

Upvotes: 2

Views: 3410

Answers (1)

BalusC
BalusC

Reputation: 1108632

It's because you're instructing the component to fire two ajax requests on the same (default) event. One <p:ajax> counts as one request. In other words, the code is behaving exactly as you programmed it.

You need to solve your concrete problem differently.

  1. Invoke one method which in turn delegates to the desired methods.

    <p:commandButton ... action="#{bean.methodWhichInvokesBothMethods()}" partialSubmit="true" />
    
  2. Invoke multiple methods in one call. You can use <f:actionListener binding> trick for this.

    <p:commandButton ... partialSubmit="true">
        <f:actionListener binding="#{bean.method1()}" />
        <f:actionListener binding="#{bean.method2()}" />
    </p:commandButton>
    

Either way, the <p:ajax> is totally unnecessary as the <p:commandButton> by itself already has builtin ajax support.

See also:

Upvotes: 4

Related Questions